5 ms·
My personal favourite Haskell one-liner is using scan for the fibonacci sequence: fibs = 1 : scanl (+) 1 fibs
by jbri 15y ago
My personal favourite Haskell one-liner is using scan for the fibonacci sequence:
fibs = 1 : scanl (+) 1 fibs
- SomeIdiot 15y agoCould you explain what it does?
- jbri 15y agoscan 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.
- vbrhjajcn 15y agoOr > fibs = 1 : 1 : zipWith (+) fibs (tail fibs) Which IMO is easier to grasp intuitively.