Re: A style question



My first try:


(loop :for i :from 1 :upto 100
:doing (cond
((= 0 (mod i 3) (mod i 5)) (write-line "FizzBuzz"))
((= 0 (mod i 3)) (write-line "Fizz"))
((= 0 (mod i 5)) (write-line "Buzz"))
(t (format t "~A~%" i))))


...then..


(loop :for i :from 1 :upto 100
:doing
(let ((fizz-or-buzz nil))
(when (= 0 (mod i 3)) (princ "Fizz") (setf fizz-or-buzz t))
(when (= 0 (mod i 5)) (princ "Buzz") (setf fizz-or-buzz t))
(unless fizz-or-buzz (princ i))
(terpri)))


I suppose one could write a version of cond with a :when-no-clauses clause
or something to do some hiding. Maybe `loop' already has something? Well,
gonna try:


(defmacro cond* (&body body)
`(let ((any-clause-p nil))
,@(mapcar (lambda (clause-form)
(if (eq :unless-till-now (first clause-form))
`(unless any-clause-p ,(second clause-form))
`(when ,(first clause-form)
,(second clause-form)
(setf any-clause-p t))))
body)))


(loop :for i :from 1 :upto 100
:doing
(cond*
((= 0 (mod i 3)) (princ "Fizz"))
((= 0 (mod i 5)) (princ "Buzz"))
(:unless-till-now (princ i)))
(terpri))


":unless-till-now" .. not sure about the name, but I'm thinking "unless
any clause has been true up to now". So it works in the middle too.

Now, hoping that I've not made too many mistakes; is there anyone who would
like to pay me to do tasks like this? :}


--
Lars Rune Nøstdal
http://nostdal.org/
.



Relevant Pages

  • Re: list extraction from begin postion to end postion.
    ... (DEFUN SUBLIST (LISTVALUE BEGINPOS COUNTITEM) ... (DEFUN STARTPOINT (LISTVALUE BEGINPOS COUNTITEM) ... (ie. when it's NIL), return NIL. ...
    (comp.lang.lisp)
  • Re: trouble learning LISP
    ... (defun flatten (tree) ... tree) into the COND statement. ... but I still have not found an easy way to trace the values as they change. ... 2: (FLATTEN NIL) ...
    (comp.lang.lisp)
  • Re: Towers of Hanoi Problem
    ... (defun hanoi (n from-peg spare-peg to-peg) ... (cond ((zerop n) ... perhaps with different names and a slighlty different print statement. ...
    (comp.lang.lisp)
  • Re: Request for comments on LISP-newbie style
    ... Returns nil ... applying MOVE-NEG-INWARDS on the arguments. ...
    (comp.lang.lisp)
  • Re: recursion
    ... (defun foo (n) ... nil);code ... (cond ((= n 0) ... programmers (CL prefers to express simple recursions as 'LOOP or 'DO). ...
    (comp.lang.lisp)