Starting at the back:
tail returns the contents of the list with a head, for example. tail [1,2,3] β [2,3]zipWith applies the function differently to the contents of two lists, for example. zipWith (+) [1,2] [10,20] β [11,22]fibs defined as a list starting with 0 and 1, and then the result of the zipWith operation
Here is a diagram that explains what happens in zipWith
v-searching the third value of fibs fibs: [0,1,...] tail: [1,.....] -------------- sum: [1,.....] now fibs is sum together with the leading 0 and 1: v-searching the fourth value of fibs fibs: [0,1,1,.....] tail: [1,1,.......] ------------------- sum: [1,2,.......] now fibs is sum together with the leading 0 and 1: v-searching the fifth value of fibs fibs: [0,1,1,2,.....] tail: [1,1,2,.......] ---------------------- sum: [1,2,3,.......] now fibs is sum together with the leading 0 and 1: v-searching the sixth value of fibs fibs: [0,1,1,2,3,.....] tail: [1,1,2,3,.......] ------------------------ sum: [1,2,3,5,.......]
So, you can see that you can get the whole list if you act "step by step", which is possible due to Haskell's lazy behavior.
Landei
source share