Skip to content

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)))))

Notes

  • [Strings](Strings and Chars)

Sequences

Functions, destructuring

Recursion / recur

Clone this wiki locally