From abcd170b34191ee4c2ca4196a10f520bc62b0748 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Tue, 2 Nov 2010 22:55:34 -0500 Subject: [PATCH] Add recursion / factorial koan. --- src/koans/recursion.clj | 21 +++++++++++++++++++++ src/path_to_answer_sheet.clj | 4 ++++ src/path_to_enlightenment.clj | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/koans/recursion.clj diff --git a/src/koans/recursion.clj b/src/koans/recursion.clj new file mode 100644 index 0000000..7f3b2ef --- /dev/null +++ b/src/koans/recursion.clj @@ -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))) diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index b424263..8ea6c47 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -87,6 +87,10 @@ :a :b :c :d :c :d] "___" ["multiply-by-5"]} + "factorial" {"__" ["(loop [n n acc 1] + (if (zero? n) + acc + (recur (dec n) (* acc n))))"]} }) (defn replace-with [s k replacements] diff --git a/src/path_to_enlightenment.clj b/src/path_to_enlightenment.clj index 3d65865..4d24dfe 100644 --- a/src/path_to_enlightenment.clj +++ b/src/path_to_enlightenment.clj @@ -25,7 +25,8 @@ "higher_order_functions" "runtime_polymorphism" "sequence_comprehensions" - "partial_functions"]) + "partial_functions" + "recursion"]) (defn run [] (apply load (doall (map (partial str "koans/") ordered-koans)))