Re: A style question



On Wed, 28 Feb 2007 03:02:46 +0100, <job-271842874@xxxxxxxxxxxxxx> wrote:

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."


You will find the same recommendation from Peter Norwig in "Paradigms of AI programming".
Something along the line: 'loop has a syntax that looks like English, unfortunately
it is nothing like English.

This is true.
For many years most lispers avoided loop. (except simplified loop)

I think the description in the ANSI CL standard entry on 'loop is next to
unreadable. This is probably the real reason it wasn't used.
What you need to learn 'loop is tons of examples.

Then along came Peter Seibels book "Practical Common Lisp"
in particular the chapter "Loop for black belts".
I know it taught me 'loop as it probably did most of the people here.
Yes 'loop's syntax is not intuitive (though highly readable).
In my opinion it's power makes it worth while to learn.

Whether you use it or not is a matter of taste.
There are other alternatives.
There is always 'do or there is a iterator package.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
.



Relevant Pages

  • Re: while (1) vs. for ( ;; )
    ... >> Why not code it like you would read it in English: ... > glance that the loop is an infinite one; ... > represents a condition that will become true when the loop is meant to ... That program WILL terminate sometime. ...
    (comp.lang.c)
  • Re: Number of rows in xls file
    ... No problem with English. ... I think I can figure out how to stop the loop from the ... Gerry Metze ... >> data on you sheet? ...
    (microsoft.public.excel.misc)
  • Re: Plzz Explain !!
    ... The loop terminates when the middle bit evaluates to zero. ... stupid things like putting printfs in the condition. ... This is inherent inthe English language meaning of the words. ... That is not how I would talk in English about processing a file or a linked lists or many other things. ...
    (comp.lang.c)
  • Re: how do people feel about exit function from loop
    ... never mind that structured programming says you shouldn't ... Imagine a complex loop rendered into English: ... The For w Exits starts off boldly, ...
    (microsoft.public.vb.general.discussion)
  • Re: while (1) vs. for ( ;; )
    ... Because clear English is not necessarily clear C. ... represents a condition that will become true when the loop is meant to ... I assume that anyone reading my code either is familiar with C, ...
    (comp.lang.c)