Skip to content

Instantly share code, notes, and snippets.

@watsoncj
Created December 30, 2011 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watsoncj/1540799 to your computer and use it in GitHub Desktop.
Save watsoncj/1540799 to your computer and use it in GitHub Desktop.
#!/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)
a
(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