简体   繁体   中英

Clojure Library Recursion Without loop … recur

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?

是的,每次在延迟序列中强制下一个元素时,迭代被调用一次,因此没有(立即)递归,也没有堆栈消耗。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM