I had to jump on the bandwagon for this one. The task does a great job of helping you learn a language you're unfamiliar with.
So today I present you with my version of the infamous FizzBuzz game in Common Lisp. This is really the first "program" I've made in CLISP, and even though it's pretty small, I'm quite proud!!
I'd really love to know if there's an even easier way to make the same functionality. I notice a lot of repetition in terms of is-multiple
, so if anyone has any ideas how I could DRY up all that, I'd appreciate it. Also, is there a formatting standard for symbol names and such in Lisp?
fizzbuzz.lisp
(defun is-multiple (i against)
(= (mod i against) 0))
(defun num-action (i)
(cond ((and (is-multiple i 3) (is-multiple i 5)) (print "FizzBuzz"))
((is-multiple i 3) (print "Fizz"))
((is-multiple i 5) (print "Buzz"))
(T (print i))))
(dotimes (i 101) (num-action i))
Can it be cleaned up any further?
(T (print i))
, you can simply have((print i))
. \$\endgroup\$ – Joshua Taylor Aug 13 '14 at 15:31p
or-p
suffix, depending on whether they have a-
in the name, so your predicate would bedivisiblep
. Common Lisp already has azerop
function, so you'd just do(zerop (mod n divisor))
. \$\endgroup\$ – Joshua Taylor Aug 13 '14 at 15:33