4

I have a question about iterate and Clojure library funcs implemented similarly to iterate.

(defn iterate
2     "Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects"
3     {:added "1.0"
4      :static true}
5     [f x] (cons x (lazy-seq (iterate f (f x)))))

Without loop ... recur does iterate not consume its stack because it is operating on a lazy-sequence?

1 Answer 1

4

Yes, each time you force next element in lazy sequence, iterate is called once, therefore there is no (immediate) recursion, and no stack consuming.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.