diff --git a/ideaboard.txt b/ideaboard.txt index d72823f..182f309 100644 --- a/ideaboard.txt +++ b/ideaboard.txt @@ -3,13 +3,11 @@ Concepts / Language Features Agents Vars state identity lifetime -lazy sequences Metadata Tuples - syntax Pattern Matching immutability/side effects memoization -recursive list processing trampolining reflection Java interop diff --git a/src/koans/recursion.clj b/src/koans/recursion.clj index 7b7b6f1..043c73b 100644 --- a/src/koans/recursion.clj +++ b/src/koans/recursion.clj @@ -4,10 +4,15 @@ (___ (is-even? (dec n))))) (defn is-even-bigint? [n] - (loop [n n acc true] - (if (= n 0) __ + (loop [n n + acc true] + (if (= n 0) + __ (recur (dec n) (not acc))))) +(defn recursive-reverse [coll] + __) + (defn factorial [n] __) @@ -21,6 +26,12 @@ "Having too many stack frames requires explicit tail calls with recur" (= false (is-even-bigint? 100003N)) + "Reversing directions is easy when you have not gone far" + (= '(1) (recursive-reverse [1])) + + "Yet more difficult the more steps you take" + (= '(5 4 3 2 1) (recursive-reverse [1 2 3 4 5])) + "Simple things may appear simple." (= 1 (factorial 1)) diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index c44f04a..eabee66 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -115,8 +115,15 @@ 'multiply-by-5 '(comp dec square)]} - "recursion" {"__" [true 'acc - '(loop [n n acc 1] + "recursion" {"__" [true + 'acc + '(loop [coll coll + acc ()] + (if (seq coll) + (recur (rest coll) (conj acc (first coll))) + acc)) + '(loop [n n + acc 1] (if (zero? n) acc (recur (dec n) (* acc n))))]