I'm trying to understand the execution of the following code:
(def fibs
(concat (lazy-seq [0 1]) (lazy-seq (map + fibs (rest fibs)))))
This is what I would expect the execution to look like
[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 ....
Which is obviously incorrect, since the result is wrong. The only execution I could come up with that produced the correct result is this:
[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 ....
Is this a correct "representation" of the state of head and tail during the execution? If so, why does (rest fibs)
return a single item? Is it because of a recursive call like (rest (rest (rest [1 1 2 3])))?