Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env clj
(println "traditional flow control solution to FizzBuzz")
(defn divides
"check if x is divisible by y without remainder"
[x y]
(= (rem x y) 0))
(loop [i 1]
(if (divides i 15)
(println "FizzBuzz")
(if (divides i 3)
(println "Fizz")
(if (divides i 5)
(println "Buzz")
(println i))))
(if (< i 100)
(recur (inc i)))
(println "dispatch table solution to FizzBuzz")
(def dispatch {1 (fn [i] (println i))
3 (fn [i] (println "Fizz"))
5 (fn [i] (println "Buzz"))
15 (fn [i] (println "FizzBuzz"))})
(defn gcd
"euclid method of calculating greatest common divisor"
[a b]
(if (= b 0)
(recur b (rem a b))))
(loop [i 1]
((get dispatch (gcd i 15)) i)
(if (< i 100)
(recur (inc i)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.