Re: A style question



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
.



Relevant Pages

  • Re: A style question
    ... but I would use loop and organize the body of the loop a bit ... (when fizz (princ "Fizz")) ... In "ANSI Common Lisp", ... I mean, it /is/ a wiki, but I would be happy to play editor and pick out the sound bites from anyone not yet in the highlight film. ...
    (comp.lang.lisp)
  • How to Speed up Cellular Automata [was Re: How to speed up an OpenGL application?]
    ... In the loop you hit aref a dozen times, god knows what else, without an iota of optimization. ... Lisp does more by default, so if you need to write ... (if (zerop cell) ...
    (comp.lang.lisp)
  • Re: A style question
    ... "The loop macro was originally designed to help inexperienced Lisp ... than its designers ever intended...to understand it in the abstract is ... I started playing with it, but I am just an elder, not a Lisp elder. ...
    (comp.lang.lisp)
  • Re: Very poor Lisp performance
    ... Observe LOOP, ... than many Lisp users, as LOOP is offensive to a percentage of Lisp ... >>> natural and programming languages have complicated grammars precisely ... do you agree that languages are evolving to be more concise? ...
    (comp.lang.lisp)
  • efficiently accumulating values
    ... do (loop as j from 0 to (1- n) ... do (let ((old (aref matrix i j))) ... acc (rec ni nj word acc) ... Is there an efficient way to accumulate values in lisp? ...
    (comp.lang.lisp)