[Haskell-beginners] Re: Understanding cached fibonnacci function

Ertugrul Soeylemez es at ertes.de
Fri Jan 30 09:59:02 EST 2009


Daniel Fischer <daniel.is.fischer at web.de> wrote:

> Not to forget the marvellous
>
> import Control.Monad.Fix
>
> fibs :: [Integer]
> fibs = fix ((0:) . scanl (+) 1)

I don't like that one.  My favorite is the following, because it's
short, concise and still very comprehensible:

  import Data.Function

  fibs :: Num i => [i]
  fibs = fix (\r x y -> x : r y (x+y)) 0 1

Replace 0 by 1, if you want to exclude it from the sequence.  I prefer
to include it.


Greets,
Ertugrul.


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




More information about the Beginners mailing list