Add some background knowledge to the recursion section.
This commit is contained in:
parent
cf4162ece6
commit
345df3d6d0
@ -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]
|
(defn factorial [n]
|
||||||
__)
|
__)
|
||||||
|
|
||||||
(meditations
|
(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."
|
"Simple things may appear simple."
|
||||||
(= 1 (factorial 1))
|
(= 1 (factorial 1))
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@
|
|||||||
10 5
|
10 5
|
||||||
30 2
|
30 2
|
||||||
15
|
15
|
||||||
20 "*"]
|
20 '*]
|
||||||
"___" ['(fn [f] (f 5))
|
"___" ['(fn [f] (f 5))
|
||||||
'(fn [f] 25)]}
|
'(fn [f] 25)]}
|
||||||
"conditionals" {"__" [:a
|
"conditionals" {"__" [:a
|
||||||
@ -63,7 +63,7 @@
|
|||||||
0
|
0
|
||||||
:glory
|
:glory
|
||||||
4 6 :your_road
|
4 6 :your_road
|
||||||
"'doom" 0 ]}
|
''doom 0 ]}
|
||||||
"higher_order_functions" {"__" [4 8 12
|
"higher_order_functions" {"__" [4 8 12
|
||||||
'(* x x)
|
'(* x x)
|
||||||
[false false true false false]
|
[false false true false false]
|
||||||
@ -90,10 +90,12 @@
|
|||||||
:a :b :c :d
|
:a :b :c :d
|
||||||
:c :d]
|
:c :d]
|
||||||
"___" ["multiply-by-5"]}
|
"___" ["multiply-by-5"]}
|
||||||
"recursion" {"__" ['(loop [n n acc 1]
|
"recursion" {"__" [true 'acc
|
||||||
|
'(loop [n n acc 1]
|
||||||
(if (zero? n)
|
(if (zero? n)
|
||||||
acc
|
acc
|
||||||
(recur (dec n) (* acc n))))]}
|
(recur (dec n) (* acc n))))]
|
||||||
|
"___" ['not]}
|
||||||
"destructuring" {"__" ["\":bar:foo\""]}
|
"destructuring" {"__" ["\":bar:foo\""]}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user