I'm having trouble understanding how one creates a lazy sequence in Clojure.
The documentation for the macro isn't at all clear to me:
Usage: (lazy-seq & body) Takes a body of expressions that returns an ISeq or nil, and yields a Seqable object that will invoke the body only the first time seq is called, and will cache the result and return it on all subsequent seq calls.
All the examples that I've seen, seem to do something like the following:
; return everything in the sequence starting at idx n
(defn myseq-after-n [n]
(...)
)
(def my-lazy-seq
(lazy-seq (conj [init-value] (myseq-after-n 2)))
)
So, the first thing I don't get is, since lazy-seq is outside the call to conj, how does it prevent conj from generating an infinite sequence at evaluation?
My second question is, do lazy sequence definitions always take this general form?