Re: A style question
- From: Lars Rune Nøstdal <larsnostdal@xxxxxxxxx>
- Date: 28 Feb 2007 02:08:39 GMT
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/
.
- Follow-Ups:
- Re: A style question
- From: Lars Rune Nøstdal
- Re: A style question
- References:
- A style question
- From: job-271842874
- A style question
- Prev by Date: Re: A style question
- Next by Date: Re: A style question
- Previous by thread: Re: A style question
- Next by thread: Re: A style question
- Index(es):
Relevant Pages
|