Would like to get step-by-step explanation of the following function in Haskell
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
I understand that the “fibs” in general is “lazy”, so next item would be calculated “on demand”, however I’m not sure how the “tail” function would work on infinite list.
So the illustration of how it works with some intermediate data would help.
At the beginning, the evalution is like this:
If we replace
fibsby its evaluation, it looks like this:Where
?denotes the unevaluated thunk. Let’s evaluate the next element offibs:The first element of each of the argument lists of
zipWithis consumed. Now, when we evaluated it, we also know, what the value of the next thunk is and we can fill it in. That allows us to evaluate the next cell, and so on: