ClojureDocs页面lazy-seq给出了生成所有正数的lazy-seq 的示例:
(defn positive-numbers
([] (positive-numbers 1))
([n] (cons n (lazy-seq (positive-numbers (inc n))))))
Run Code Online (Sandbox Code Playgroud)
这个lazy-seq可以针对相当大的索引进行评估,而不会抛出StackOverflowError(与同一页面上的筛选示例不同):
user=> (nth (positive-numbers) 99999999)
100000000
Run Code Online (Sandbox Code Playgroud)
如果只能recur用于避免在递归函数中使用堆栈帧,那么这个lazy-seq示例怎么可能看起来自己调用而不会溢出堆栈?