Re:Haskell One Liners to Impress Your Friends
by vbrhjajcn on Saturday June 04, 2011 @05:43AM
(#2619233)
Or
> fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
Which IMO is easier to grasp intuitively.
Re:Haskell One Liners to Impress Your Friends
by SomeIdiot on Saturday June 04, 2011 @04:47AM
(#2619181)
Could you explain what it does?
scan is similar to fold - except instead of the result being the final value, it's a list of all the intermediate values up to the final one (if it exists - scanl is productive and can handle infinite lists just fine).
So the first fibonacci number is 1 (we're concatenating it onto the front of the list), the second fibonacci number is also one (the second argument to scan), and every fibonacci number after that is the sum of the previous number (scan's running total) and the one before that.
fibs = 1 : scanl (+) 1 fibs