記述能力の高い Clojure.
よくあるプログラミングの演習問題を Clojure で解いてみます.
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
(require '[clojure.test :refer :all]) | |
;; fizz-buzz | |
;; 連続整数列をリストとして与えると各要素に対して | |
;; 3 の倍数は "fizz" を | |
;; 5 の倍数は "buzz" を | |
;; 3 と 5 の両方の倍数の場合は "fizzbuzz" を出力し, | |
;; それ以外は何も出力しない, という法則で出力される文字列の順序集合を | |
;; リストとして返す. | |
(defn fizz-buzz [coll] |
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
(require '[clojure.test :refer :all]) | |
(defn hanoi-states [n] | |
(letfn [ | |
(hanoi [s t d n] | |
(if (zero? n) '() | |
(concat (hanoi s d t (dec n)) | |
(list | |
(condp = [s d] | |
[0 2] (fn [[[h & a] b c]] [(if a a '()) b (cons h c)]) |
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
(use 'clojure.test) | |
(defn fold-right [f z coll] | |
(loop [[c & cs] coll rvsd '()] | |
(if (nil? c) | |
(loop [acc z [r & rs] rvsd] | |
(if r (recur (f r acc) rs) acc)) | |
(recur cs (cons c rvsd))))) | |
(defn reduce-right [f coll] |
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
(import 'java.util.Date java.text.SimpleDateFormat) | |
(defn r-reduce [f coll retn pred] | |
(loop [[c & cs :as curr] coll rvsd '()] | |
(if (or (nil? c) (pred curr)) | |
(loop [acc (retn curr) [r & rs] rvsd] | |
(if r (recur (f r acc) rs) acc)) | |
(recur cs (cons c rvsd))))) | |
(def sdf (SimpleDateFormat. "yyyy-MM-dd")) |
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
(use 'clojure.test) | |
(defn unfold | |
"A variant of 'iterate' which accepts a stopping condition, | |
having the same syntax as 'unfold' of scheme. | |
Supposed | |
(x1 x2 x3 ... xk-1 xk ... ) == (s (g s) (g (g s)) ... ) | |
and '(p xi)' returns true first at i == k, | |
((f x1) (f x2) (f x3) ... (f xk-1)) | |
is returned." |
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
(require '[clojure.test :refer :all]) | |
(def s53 | |
; If you are in 4clojure paste from here | |
(fn [coll] | |
(->> coll | |
; 1st. Make tails of coll. | |
(#(take-while identity (iterate next %))) | |
; 2nd. Take only consecutive elements from the head for each list. | |
(map (fn [[c & cs]] |
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
(defn fizz-buzz [n] | |
(map #(cond (zero? (mod % 15)) "FizzBuzz" | |
(zero? (mod % 5)) "Buzz" | |
(zero? (mod % 3)) "Fizz" | |
:else %) | |
(range 1 (inc n)))) | |
(fizz-buzz 100) | |
; -> (1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" | |
; 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz" |
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
(require '[clojure.test :refer (with-test are run-tests)]) | |
(with-test | |
(defn str-drop-while-same [& args] | |
(if (some nil? args) args | |
(->> (map #(concat % (repeat nil)) args) | |
(apply map vector) | |
(drop-while #(apply = %)) | |
(take-while #(some identity %)) | |
(apply map str)))) |
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
(require '[clojure.test :refer (with-test are run-tests)]) | |
(with-test | |
(defn awtp [cs a] | |
((fn f [[h & r] a] | |
(cond | |
(zero? a) #{{}} | |
(nil? h) #{} | |
:else (set | |
(mapcat (fn [c] |