Simpler Fibonacci function

Paul Hudak paul.hudak@yale.edu
Tue, 05 Feb 2002 13:26:44 -0500


The only reason the first version of fib was used in the Gentle Intro
was to demonstrate recursive stream processing, and not to show a
"canonical" version of Fibonacci.  Indeed, the sentence preceeding it
says: "For another example of the use of circularity, the Fibonacci
sequence can be computed efficiently as the following infinite sequence:
...".  Your proposed version is fine, and is similar to the "canonical"
version that computes the nth Fibonacci number (as opposed to the entire
stream) efficiently, but it does not demonstrate recursive streams.

Hope this helps,

  -Paul

Brian Berns wrote:
> I am new to functional programming and teaching myself Haskell.  The
> canonical Haskell "fib" function (e.g. as presented in the "Gentle"
> tutorial) is:
> 
>    fib = 1 : 1 : [ a+b | (a,b) <- zip fib (tail fib) ]
> 
> This seems, to be polite, a bit overly complex.  By comparison, here
> is a simpler version:
> 
>    fib x y = x : fib y (x+y)
> 
> For example, fib 1 1 => [1,1,2,3,5,8,13,21,...].
> 
> Is there a reason why the canonical fib function is so complex?  If
> not, would it be possible to use a simpler version in the tutorial?
> Thanks.