News for hackers. Stuff that matters.
Re:Haskell One Liners to Impress Your Friends by jbri on Saturday June 04, 2011 @04:30AM (#2619172)
My personal favourite Haskell one-liner is using scan for the fibonacci sequence:

fibs = 1 : scanl (+) 1 fibs

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?
Re:Haskell One Liners to Impress Your Friends by jbri on Saturday June 04, 2011 @04:54AM (#2619187)
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.