Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created May 7, 2012 21:45
Show Gist options
  • Select an option

  • Save fumokmm/2630671 to your computer and use it in GitHub Desktop.

Select an option

Save fumokmm/2630671 to your computer and use it in GitHub Desktop.
ClojureでFizzBuzzを書いた
;
; 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))
;
; 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))
;
; 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