From: justinhj on
(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 "~%"))))

From: job-271842874 on
job-271842874(a)craigslist.org wrote:
> Ken Tilton wrote:
> Gotcha. Well, I just posted a reply to Paul with a generalization of
> passing in a 2nd list parameter containing pairs of (integer, string)
> with the intent of concatenating the strings for all integers that
> evenly divide i.
>

Oops, I guess it's getting late. The output doesn't match the list - I
mistakenly used the following instead when I ran the program:
(fizz-buzz 12 '((2 "a") (3 "b") (4 "c")))
that's why 4 is ac and 12 is abc ...

> So for the list '((2 "a")(3 "b")(5 "c")) we'd have
> 1
> a
> b
> ac
> 5
> ab
> 7
> ac
> b
> a
> 11
> abc
>
> So, 45 would just be "FizzBuzz"
>
>>
>> I have had managers lunge acroos the table to get at me.
>>
>> kt
>>
From: job-271842874 on
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]>
From: Dan Bensen on
Paul Wallich wrote:
> In both cases, what the local variables add in supposed elegance seems
> to me lost by the clunkiness of setting them in the first place
> I'd probably brute-force the problem with a simple cond

You can eliminate variables and repetition by making the string
do double duty:

(let ((str (concatenate 'string
""
(if (zerop (mod n 3)) "fizz" "")
(if (zerop (mod n 5)) "buzz" ""))))
(write-line
(if (string= str "") (format nil "~D" n) str)))

--
Dan
www.prairienet.org/~dsb
From: GP lisper on
On 27 Feb 2007 18:38:28 -0800, <pillsbury(a)gmail.com> wrote:
> On Feb 27, 9:02 pm, job-271842...(a)craigslist.org wrote:
>
>> "The loop macro was originally designed to help inexperienced
>> Lisp users write iterative code...Unfortunately, loop is more like
>> ...
>
>> Is this a minority view?
>
> I think it is. I cordially despise LOOP myself, but almost everyone
> uses it...
>
> You should spend some time becoming familiar with it.

When I needed to learn loop, I found that by reading the chapter in
Common Lisp, the Language I gained the best understanding.

Loop is a Domain Specific Language, something that many loop haters
overlook, as they will also cheer DSLs as a Lisp strength.

--
There are no average Common Lisp programmers
Reply-To: email is ignored.

--
Posted via a free Usenet account from http://www.teranews.com