Re: A style question
- From: job-271842874@xxxxxxxxxxxxxx
- Date: Tue, 27 Feb 2007 21:02:46 -0500
Frank Buss wrote:
job-271842874@xxxxxxxxxxxxxx wrote:
I cranked out a Lisp version. It seems a bit clunky, so I thought I'd see if anyone had suggestions for improvements.
looks ok, but I would use loop and organize the body of the loop a bit
different:
(defun fizz-buzz (n)
(loop for i from 1 to n
for fizz = (zerop (mod i 3))
for buzz = (zerop (mod i 5)) do
(when fizz (princ "Fizz"))
(when buzz (princ "Buzz"))
(when (not (or fizz buzz)) (princ i))
(terpri)))
Thanks. Some questions/comments:
1) In "ANSI Common Lisp", Graham makes the following comments:
"The loop macro was originally designed to help inexperienced Lisp users write iterative code...Unfortunately, loop is more like English than its designers ever intended...to understand it in the abstract is almost impossible...For such reasons, the use of loop cannot be recommended."
Is this a minority view? One of the things that attracted me to Lisp was the simplicity, consistency, etc. of the language, so when I read the above, it seemed reasonable.
2) Thanks for the zerop tip. I like (zerop (mod m n)) better than (= 0 (mod m n))
3) Any reason why you chose (when fizz (princ "Fizz")) instead of (if fizz (princ "Fizz")) ?
4) Curious about the history of "terpri" - I guess it's shorter than "newline" but not very intuitive :)
I'm really enjoying learning Lisp. I realize at this stage I still have some "the grass is greener" and "oh cool, something new to learn!" influences, but I expect as that wears off the merits of the language will continue to shine through.
Brian
.
- Follow-Ups:
- Re: A style question
- From: Frank Buss
- Re: A style question
- From: John Thingstad
- Re: A style question
- From: Tim Bradshaw
- Re: A style question
- From: Vassil Nikolov
- Re: A style question
- From: Ken Tilton
- Re: A style question
- From: Pillsy
- Re: A style question
- References:
- A style question
- From: job-271842874
- Re: A style question
- From: Frank Buss
- 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
|