我试图了解以下代码的执行:
(def fibs
(concat (lazy-seq [0 1]) (lazy-seq (map + fibs (rest fibs)))))
这就是我希望执行的样子
[0 1 : (map + [0 1] [1]) => 1
[0 1 1 : (map + [0 1 1] [1 1]) => 1 2
[0 1 1 1 2 : (map + [0 1 1 2] [1 1 2]) => 1 2 3
[0 1 1 1 2 1 2 3 : (map + [0 1 1 2 3] [1 1 2 3]) => 1 2 3 5
[0 1 1 1 2 1 2 3 1 2 3 5 ....
这显然是不正确的,因为结果是错误的。我能想到的唯一产生正确结果的执行是:
[0 1 : (map + [0 1] [1]) => 1
[0 1 1 : (map + [1 1] [1]) => 2
[0 1 1 2 : (map + [1 2] [2]) => 3
[0 1 1 2 3 : (map + [2 3] [3]) => 5
[0 1 1 2 3 5 ....
这是执行过程中头部和尾部状态的正确“表示”吗?如果是这样,为什么(rest fibs)
返回单个项目?是因为像 (rest (rest (rest [1 1 2 3]))) 这样的递归调用吗?