Strict<->Lazy?

G.W.M. Vissers vissers@theochem.kun.nl
Fri, 2 Jul 1999 09:06:58 +0200 (MET DST)


> 	fib :: Int -> Int
> 	fib 0 = 0
> 	fib 1 = 1
> 	fib n = fib (n-1)+fib(n-2)
> 	
> Er, fib 0 is ONE, not ZERO.

No it's not. At least, Maple gives 0 for fibonacci(0). Besides that, it
doesn't really matter if you make it zero, it's just that the whole series
shifts one number to the right.
> 
> I haven't cared to try fib(40) because I'm using an 84MHz machine
> and fib(35) is already interesting.  Interestingly, this definition
> took 6.8 seconds for fib(35), whereas 
> 	fib n = if (n < 2) 1 (fib (n-1) + fib (n-2))
> took 5.7 seconds.

That's certainly interesting, because when using

fib3 :: Int -> Int
fib3 n = if (n < 2) n (fib (n-1) + fib(n-2))

it takes my computer about 23.5 seconds to compute the 40-th number,
making it 2 seconds slower than the original algorithm. Making it strict
doesn't seem to help.

[snip]
> also computing non-Fibs thanks
> to the 0 that should have been 1); 

Not computing non-Fibs! The zero doesn't have any effect, except for the
shift.