I'm learning about lazy seqs at the moment, and I've noticed that they usually involve recursion without using recur
. For example, here is the implementation of iterate
:
(defn iterate
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects"
{:added "1.0"
:static true}
[f x] (cons x (lazy-seq (iterate f (f x)))))
I was under the impression that if you recurse without using recur
, it will make a new stack frame for every call, which can cause stack overflows if you iterate enough times.
Does lazy-seq
gobble up stack frames? If not, how does it avoid that?