From: Alain Picard on 3 Mar 2007 01:38 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.] --ap
From: Lars Rune Nøstdal on 3 Mar 2007 18:57 On Sat, 03 Mar 2007 23:56:43 +0000, Lars Rune Nøstdal wrote: > On Sat, 03 Mar 2007 17:38:02 +1100, Alain Picard wrote: > >> 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. > > I am aware of this. However I very much prefer using keywords here because > I can easily separate stuff then. Emacs highlights keywords with a > different color making it even clearer. > What I should have done was use `zerop' though. :) -- Lars Rune Nøstdal http://nostdal.org/
From: Lars Rune Nøstdal on 3 Mar 2007 18:56 On Sat, 03 Mar 2007 17:38:02 +1100, Alain Picard wrote: > 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. I am aware of this. However I very much prefer using keywords here because I can easily separate stuff then. Emacs highlights keywords with a different color making it even clearer. -- Lars Rune Nøstdal http://nostdal.org/
From: christopher.m.russell on 3 Mar 2007 19:38 On Mar 1, 6:24 am, Vassil Nikolov <vnikolov+use...(a)pobox.com> wrote: > Note: we do want to do each test exactly once for elegance, even if > the predicates are tabulated. > > ---Vassil. Alternatively we could tabulate the responses instead... (defun my-bool-hash-fun (bools) (let ((acc 0)) (labels ((hash-fn (list) (setf acc (+ (* 2 acc) (if (first list) 1 0))) (when (rest list) (hash-fn (rest list))))) (hash-fn bools) acc))) (defun make-response (&rest lambdas) (let ((fn-vector (make-array (length lambdas) :initial-contents lambdas))) (lambda (&rest rest) (aref fn-vector (my-bool-hash-fun rest))))) (let ((temp (make-response (lambda (x)(format t "~a~%" x)) (lambda (x)(declare (ignore x))(format t "Foo~%")) (lambda (x)(declare (ignore x))(format t "Bar~%")) (lambda (x)(declare (ignore x))(format t "FooBar~%"))))) (defun foobar (&rest rest) (apply temp rest))) (dotimes (k 100) (funcall (foobar (zerop(mod k 3)) (zerop(mod k 5)))k))
From: ctnd on 4 Mar 2007 10:44
Someone had to come up with the horrible solution: (defun fb (start finish tests otherwise) (loop for n from start to finish for tested-t = nil do (loop for test in tests while (if (funcall (first test) n) (progn (funcall (second test) n) (setf tested-t t) nil) t)) (if (not tested-t) (funcall otherwise n))))) (defun fizz-buzz-example () (fb 1 100 (list (list #'(lambda (n) (and (zerop (mod n 3)) (zerop (mod n 5)))) #'(lambda (n) (print "FizzBuzz"))) (list #'(lambda (n) (zerop (mod n 3))) #'(lambda (n) (print "Fizz"))) (list #'(lambda (n) (zerop (mod n 5))) #'(lambda (n) (print "Buzz")))) #'print)) |