Skip to content

Commit

Permalink
Add some background knowledge to the recursion section.
Browse files Browse the repository at this point in the history
  • Loading branch information
trptcolin committed Nov 7, 2010
1 parent cf4162e commit 345df3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/koans/recursion.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
(defn is-even? [n]
(if (= n 0) __
(___ (is-even? (dec n)))))

(defn is-even-bigint? [n]
(loop [n n acc true]
(if (= n 0) __
(recur (dec n) (not acc)))))

(defn factorial [n]
__)

(meditations
"Recursion ends with a base case"
(= true (is-even? 0))

"And starts by moving toward that base case"
(= false (is-even? 1))

"Having too many stack frames requires explicit tail calls with recur"
(= false (is-even-bigint? 100003N))

"Simple things may appear simple."
(= 1 (factorial 1))

Expand Down
10 changes: 6 additions & 4 deletions src/path_to_answer_sheet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@
10 5
30 2
15
20 "*"]
20 '*]
"___" ['(fn [f] (f 5))
'(fn [f] 25)]}
"conditionals" {"__" [:a
[]
0
:glory
4 6 :your_road
"'doom" 0 ]}
''doom 0 ]}
"higher_order_functions" {"__" [4 8 12
'(* x x)
[false false true false false]
Expand All @@ -90,10 +90,12 @@
:a :b :c :d
:c :d]
"___" ["multiply-by-5"]}
"recursion" {"__" ['(loop [n n acc 1]
"recursion" {"__" [true 'acc
'(loop [n n acc 1]
(if (zero? n)
acc
(recur (dec n) (* acc n))))]}
(recur (dec n) (* acc n))))]
"___" ['not]}
"destructuring" {"__" ["\":bar:foo\""]}
})

Expand Down

0 comments on commit 345df3d

Please sign in to comment.