Permalink
Browse files

Add a recursive list processing example.

  • Loading branch information...
1 parent 7d9fb04 commit 1dd2e9ca86c8f71a4fd78d8aa3373ceb65d311d4 @trptcolin trptcolin committed Mar 31, 2011
Showing with 22 additions and 6 deletions.
  1. +0 −2 ideaboard.txt
  2. +13 −2 src/koans/recursion.clj
  3. +9 −2 src/path_to_answer_sheet.clj
View
@@ -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
View
@@ -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))
@@ -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))))]

0 comments on commit 1dd2e9c

Please sign in to comment.