; holds the head (avoid!) (def head-fibo (lazy-cat [0N 1N] (map + head-fibo (rest head-fibo))))
lazy sequence plus recursion 定义了 Fibonaccis,简洁(虽然不实用)到我无法理解代码执行过程的地步。
求指教。
首先,lazy-cat 的用法为: (lazy-cat & colls) (lazy-cat xs ys) === (concat (lazy-seq xs) (lazy-seq ys)) 在你的那句代码中, xs 是 [0N 1N] ,这是 head-fibo 头两个元素 ys 是 (map + head-fibo (rest head-fibo))
(lazy-cat xs ys) === (concat (lazy-seq xs) (lazy-seq ys))
[0N 1N]
(map + head-fibo (rest head-fibo))
其中 head-fibo 是 (lazy-seq [0N, 1N, ....] (rest head-fibo) 是 (lazy-seq [1N, ....]
(lazy-seq [0N, 1N, ....]
(lazy-seq [1N, ....]
map + 操作把两个序列对应元素相加合成一个序列 不断的计算,可以得到 (map + head-fibo (rest head-fibo)) 等于 (lazy-seq [1N, 2N, 3N, 5N, ...])
(lazy-seq [1N, 2N, 3N, 5N, ...])
#1 楼 @discover 亲,不断计算的过程才是重点。这个问题我在 Reddit 上得到解答。请参考。http://www.reddit.com/r/Clojure/comments/30vukq/newbie_question_from_programming_clojure_2nd/
#1 楼 @discover Thank you anyway. Have a lambda smile λ_λ.
#2 楼 @turristan