From: justinhj on
On Feb 27, 11:31 pm, job-271842...(a)craigslist.org wrote:
> justinhj wrote:
> > (defun multiple(x n)
> > (= 0 (mod x n)))
>
> > (defun output-multiple(x n str)
> > (if (and (multiple x n) (princ str))
> > 1
> > 0))
>
> > (defun fizzbuzz(n)
> > (loop for x from 1 to n do
> > (if (> (+ (output-multiple x 3 "fizz") (output-multiple x 5
> > "buzz")) 0)
> > (format t "~%"))))
>
> Hmm.. this doesn't seem to work.
>
> [1]> (defun multiple(x n)
> (= 0 (mod x n)))
> MULTIPLE
> [2]>
> (defun output-multiple(x n str)
> (if (and (multiple x n) (princ str))
> 1
> 0))
> OUTPUT-MULTIPLE
> [3]>
> (defun fizzbuzz(n)
> (loop for x from 1 to n do
> (if (> (+ (output-multiple x 3 "fizz") (output-multiple x 5
> "buzz")) 0)
> (format t "~%"))))
> FIZZBUZZ
> [4]> (fizzbuzz 12)
> fizz
> buzz
> fizz
> fizz
> buzz
> fizz
> NIL
> [5]>

Unless I'm misunderstanding something what doesn't work? The output is
correct.

Justin


From: Barry Fishman on
"Tim Bradshaw" <tfb+google(a)tfeb.org> writes:
> Incidentally, I'm deeply disappointed in the quality of answers in
> this thread. In the elder days there would have been at least a few
> followups showing how to do this in the proper "FORMAT string
> indistinguishable from line noise" way. No true CL programmer ever
> uses any other construct when the problem can be solved with a
> combination of FORMAT, LOOP & GO (FORMAT being always preferable,
> obviously). There may yet be those reading cll who know this, though
> I suspect they have all gone into the west now.

I'm not a good practitioner of FORMAT, but there needs to be at least
one FORMAT solution presented.

(defun fizz-buzz (n)
(loop for i from 1 to n do
(format t "~[~d~;Fizz~;Buzz~;FizzBuzz~]~%"
(+ (if (zerop (mod i 3)) 1 0)
(if (zerop (mod i 5)) 2 0))
i)))

--
Barry Fishman
From: justinhj on
On Feb 28, 10:51 am, "justinhj" <justi...(a)gmail.com> wrote:
> On Feb 27, 11:31 pm, job-271842...(a)craigslist.org wrote:
>
>
>
> > justinhj wrote:
> > > (defun multiple(x n)
> > > (= 0 (mod x n)))
>
> > > (defun output-multiple(x n str)
> > > (if (and (multiple x n) (princ str))
> > > 1
> > > 0))
>
> > > (defun fizzbuzz(n)
> > > (loop for x from 1 to n do
> > > (if (> (+ (output-multiple x 3 "fizz") (output-multiple x 5
> > > "buzz")) 0)
> > > (format t "~%"))))
>
> > Hmm.. this doesn't seem to work.
>
> > [1]> (defun multiple(x n)
> > (= 0 (mod x n)))
> > MULTIPLE
> > [2]>
> > (defun output-multiple(x n str)
> > (if (and (multiple x n) (princ str))
> > 1
> > 0))
> > OUTPUT-MULTIPLE
> > [3]>
> > (defun fizzbuzz(n)
> > (loop for x from 1 to n do
> > (if (> (+ (output-multiple x 3 "fizz") (output-multiple x 5
> > "buzz")) 0)
> > (format t "~%"))))
> > FIZZBUZZ
> > [4]> (fizzbuzz 12)
> > fizz
> > buzz
> > fizz
> > fizz
> > buzz
> > fizz
> > NIL
> > [5]>
>
> Unless I'm misunderstanding something what doesn't work? The output is
> correct.
>
> Justin

I'm not having much luck trying to make it elegant and concise but I
quite like this recursive version...

(defun fizzbuzz2(n)
(labels ((fizzbuzz-rec (current last fizz buzz)
(let ((output nil))
(if (= 0 fizz)
(push "fizz" output))
(if (= 0 buzz)
(push "buzz" output))
(if output
(format t "~{~a~}~%" output))
(unless (= current last)
(fizzbuzz-rec (1+ current) last (mod (1-
fizz) 3) (mod (1- buzz) 5))))))
(fizzbuzz-rec 1 n (1- 3) (1- 5))))

CL-USER> (fizzbuzz2 15)
fizz
buzz
fizz
fizz
buzz
fizz
buzzfizz


Justin

From: Raffael Cavallaro on
On 2007-02-28 02:33:08 -0500, job-271842874(a)craigslist.org said:

> Looks like read macros are involved, but I don't want to get ahead of myself...

To clarify, rather than use mod, Rob's version has loop iterate on two
circular lists, one representing (mod i 3) the other representing (mod
i 5). The #integer= and #integer# notations allow the lisp reader to
construct wholly or partially self-referential list structure. Heed
Rob's warning about setting *print-circle* to t unless you want a stack
overflow.

From: Tim Bradshaw on
On Feb 28, 3:40 pm, Richard M Kreuter <kreu...(a)progn.net> wrote:

> (dotimes (i 100)
> (format t "~[~[FizzBuzz~:;Fizz~]~:;~[Buzz~:;~D~]~]~%" (mod i 3) (mod i 5) i))
>
> Best I could do on one cup of coffee.

Good, but you'd get extra points for avoiding the explicit FizzBuzz in
there. Off the top of my head I think:
(format t "~&~[Fizz~;~]~[Buzz~;~D~]~%" (mod i 3) (mod i 5) i). But
I'd have to check, and it's not really squiggly enough.

--tim