Created
May 7, 2012 21:45
-
-
Save fumokmm/2630671 to your computer and use it in GitHub Desktop.
ClojureでFizzBuzzを書いた
This file contains hidden or 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
| ; | |
| ; fizzbuzz1 | |
| ; | |
| (def x | |
| (for [i (range 1 101)] | |
| (cond | |
| (and | |
| (= (mod i 3) 0) | |
| (= (mod i 5) 0)) "FizzBuzz" | |
| (= (mod i 3) 0) "Fizz" | |
| (= (mod i 5) 0) "Buzz" | |
| :else (str i)))) | |
| (doseq [n x] (println n)) |
This file contains hidden or 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
| ; | |
| ; fizzbuzz2 | |
| ; | |
| (defn one-to-onehandred | |
| [] | |
| (map | |
| (fn [x] | |
| (cond | |
| (and | |
| (= (mod x 3) 0) | |
| (= (mod x 5) 0)) "FizzBuzz" | |
| (= (mod x 3) 0) "Fizz" | |
| (= (mod x 5) 0) "Buzz" | |
| :else x)) | |
| (map #(+ % 1) (range 100)) | |
| ) | |
| ) | |
| (doseq [x (map #(str % "\n") (one-to-onehandred))] | |
| (print x)) |
This file contains hidden or 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
| ; | |
| ; fizzbuzz3 | |
| ; | |
| ; 1-100までのリストを生成 | |
| (def one-to-onehandred (map inc (range 100))) | |
| ; FizzBuzz文字列にマッピング | |
| (def fizzbuzz-list | |
| (map | |
| (fn [x] | |
| (cond | |
| (= (mod x 15) 0) "FizzBuzz" | |
| (= (mod x 3) 0) "Fizz" | |
| (= (mod x 5) 0) "Buzz" | |
| :else (str x))) one-to-onehandred)) | |
| ; 出力 | |
| (doseq [x fizzbuzz-list] (println x)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment