-
Notifications
You must be signed in to change notification settings - Fork 0
Recursion
David Soroko edited this page Jan 14, 2017
·
3 revisions
Your standard size of a sequence. Second version uses recur and is a tail recursive. The pattern is (loop [x x-initial-value, y y-initial-value] (do-something-with x y)). Initially loop binds the variables in the even positions to the values in the odd positions.
(defn size [v]
(if (empty? v)
0
(inc (size (rest v)))))
(defn size [v]
(loop [l v, ctr 0]
(if (empty? l)
ctr
(recur (rest l) (inc ctr)))))