Created
December 30, 2011 17:55
-
-
Save watsoncj/1540799 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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