Add recursion / factorial koan.
This commit is contained in:
parent
87e25afc1e
commit
abcd170b34
21
src/koans/recursion.clj
Normal file
21
src/koans/recursion.clj
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
(defn factorial [n]
|
||||||
|
__)
|
||||||
|
|
||||||
|
(meditations
|
||||||
|
"Simple things may appear simple."
|
||||||
|
(= 1 (factorial 1))
|
||||||
|
|
||||||
|
"They may require other simple steps."
|
||||||
|
(= 2 (factorial 2))
|
||||||
|
|
||||||
|
"Sometimes a slightly bigger step is necessary"
|
||||||
|
(= 6 (factorial 3))
|
||||||
|
|
||||||
|
"And eventually you must think harder"
|
||||||
|
(= 24 (factorial 4))
|
||||||
|
|
||||||
|
"You can even deal with very large numbers"
|
||||||
|
(= (factorial 1000N) (factorial 1000N))
|
||||||
|
|
||||||
|
"But what happens when the machine limits you?"
|
||||||
|
(= (factorial 10000N) (factorial 10000N)))
|
@ -87,6 +87,10 @@
|
|||||||
:a :b :c :d
|
:a :b :c :d
|
||||||
:c :d]
|
:c :d]
|
||||||
"___" ["multiply-by-5"]}
|
"___" ["multiply-by-5"]}
|
||||||
|
"factorial" {"__" ["(loop [n n acc 1]
|
||||||
|
(if (zero? n)
|
||||||
|
acc
|
||||||
|
(recur (dec n) (* acc n))))"]}
|
||||||
})
|
})
|
||||||
|
|
||||||
(defn replace-with [s k replacements]
|
(defn replace-with [s k replacements]
|
||||||
|
@ -25,7 +25,8 @@
|
|||||||
"higher_order_functions"
|
"higher_order_functions"
|
||||||
"runtime_polymorphism"
|
"runtime_polymorphism"
|
||||||
"sequence_comprehensions"
|
"sequence_comprehensions"
|
||||||
"partial_functions"])
|
"partial_functions"
|
||||||
|
"recursion"])
|
||||||
|
|
||||||
(defn run []
|
(defn run []
|
||||||
(apply load (doall (map (partial str "koans/") ordered-koans)))
|
(apply load (doall (map (partial str "koans/") ordered-koans)))
|
||||||
|
Loading…
Reference in New Issue
Block a user