From: Wade Humeniuk on 4 Mar 2007 11:54 ctnd wrote: > Someone had to come up with the horrible solution: There is nothing wrong with that solution. Its legit. Here is another generalized solution using the approach I previously used. I removed MOD as it is not needed. (defun fbv (i map) (or (remove nil (mapcar (lambda (mod) (and (integerp (/ i (car mod))) (cadr mod))) map)) (list i))) (defun fizzbuzz (n map) (loop for i from 1 to n do (format t "~{~A~}~%" (fbv i map)))) CL-USER 21 > (fizzbuzz 15 '((3 "Fizz") (5 "Buzz") (8 "Gack"))) 1 2 Fizz 4 Buzz Fizz 7 Gack Fizz Buzz 11 Fizz 13 14 FizzBuzz NIL CL-USER 23 > (fbv 40 '((3 "Fizz") (5 "Buzz") (8 "Gack"))) ("Buzz" "Gack") CL-USER 24 > (fbv 120 '((3 "Fizz") (5 "Buzz") (8 "Gack"))) ("Fizz" "Buzz" "Gack") CL-USER 25 > (fbv 49 '((3 "Fizz") (5 "Buzz") (8 "Gack"))) (49) CL-USER 26 >
From: Pascal Bourguignon on 6 Mar 2007 04:47
Alain Picard <Alain.Picard(a)memetrics.com> writes: > Lars Rune N�stdal <larsnostdal(a)gmail.com> writes: > >> My first try: >> >> >> (loop :for i :from 1 :upto 100 >> :doing (cond >> ((= 0 (mod i 3) (mod i 5)) (write-line "FizzBuzz")) >> ((= 0 (mod i 3)) (write-line "Fizz")) >> ((= 0 (mod i 5)) (write-line "Buzz")) >> (t (format t "~A~%" i)))) >> > > Stylistic hint: you don't need all those keywords in there. > (loop for i from 1 ... does just fine. > > [Does anyone know where this bizarre habit of using keywords > for loop operators came from? It seems relatively new.] Apart from the special font-locking done by emacs on the keywords mentionned by Lars, the point of using keywords in LOOP is to avoid symbol name colision when, like me, you have a package that exports macros named WHILE or UNTIL, that you may use (like in USE-PACKAGE) after writting a LOOP. Note that WHILE is NOT exported from CL, to the difference from LOOP or DOLIST. -- __Pascal Bourguignon__ http://www.informatimago.com/ Un chat errant se soulage dans le jardin d'hiver Shiki |