I saw this implementation of the Fibonacci numbers in Haskell and I am still trying to figure out why this works properly. So apperently, the Fibonacci numbers can be written in a very compact way using the zipWith function. The implementation looks like the following
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
To understand better what is happening here I looked at the documentation of the zipWith function. This function adds two lists [a], [b] together using the given function (a -> b -> c). In our case the function is a simple addition. If the two lists [a] and [b] have different length (in our case list [b] is always one element shorted than list [a]) zipWith simply starts at the beginning of both lists and adds them. If the end of one list is reached it just stops no matter if the end of the other list is already reached.
In the first step of the recursion zipWith is called with [0,1] and tail[0,1] = [1]. This leads to another 1 => [0, 1, 1]. In the second step of the recursion zipWith gets called with [0,1,1] and [1,1] resulting in [0+1,1+1] = [1,2]. So for me it is clear that the recursion creates the right fibonacci numbers but I don't fully understand why only the last number after the zipWith step is added to the result and not the whole list. Maybe someone can explain it to me. That would be super helpful. Thank you very much.