我很难理解如何在Clojure中创建懒惰序列。
宏的文档对我来说一点也不清楚:
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.
我看到的所有例子似乎都有以下作用:
; 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)))
)
所以,我不知道的第一件事是,既然lazy-seq在对conj的调用之外,它如何防止conj在求值时生成无限序列?
我的第二个问题是,惰性序列定义总是采用这种通用形式吗?