Computer Science Canada

Haskell Brain Pain: Fibonacci Numbers

Author:  wtd [ Thu Sep 08, 2005 5:45 pm ]
Post subject:  Haskell Brain Pain: Fibonacci Numbers

code:
fibonacci = 1 : 1 : zipWith (+) fibonacci (tail fibonacci)


Explain how this manages to generate all fibonacci numbers. Smile

Author:  rizzix [ Thu Sep 08, 2005 6:40 pm ]
Post subject: 

ehm easy... it appends the zip of the sum of the nth and (n+1)th element of the list L=1:1:... to list L, through lazy evaluation.. and you get ur fibonacci sequence

that's one way of looking at it.. truth is.. it lazily evaluates the rest of the list.. nothing is appended.


: