Strict<->Lazy?

G.W.M. Vissers vissers@theochem.kun.nl
Thu, 1 Jul 1999 09:29:31 +0200 (MET DST)


I have read your contribution to this list, and was surprised that it took
Clean 30 seconds to compute the 40-th Fibonacci number. I thought that was
impossible, so I wrote a tiny program to check. To my astonishment you
were right when using a simple Fibonacci algorith, like

fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1)+fib(n-2)

This took my computer about 21.5 seconds to calaculate the 40-th number.
But when rewriting the algorithm to

fib2 :: Int -> Int
fib2 n = fibList !! n
where
  fibList = [0,1:[i+j \\ i <- fibList & j <- tl fibList]]

the computing time became negligible. When changing the output to reals
even the 1000-th fibonacci number took virtually no time to compute. 

> Dear reader,
> 
> I am new in the field of functional programming. I would like to learn
> Clean. But I do not know if I should enter the door in that field.
> 
> Yesterday I wrote a little program for calculating the fibonacci
> numbers. I do know from Maple that, e.g. evaluating the fibonacci number
> 40 is done in "real time" (and also e.g. the fibonacci number 100 is
> done in real time; be aware that this is not a built in function). And
> also Omikron Basic 6.3 (Basic for the Macintosh) makes the job in "real
> time". And then I was shocked, because Clean requires 30sec! (and
> curiously calculating the fibonacci number with real number is not
> faster; the opposite is the case; maybe this is due to the fact that my
> Macintosh 603e processor is good for integers).
> 
> After that I printed out the "Clean Language Report". I found in it that
> it is possible to make a "strict" evaluation. But calculating the
> fibonacci numbers in a strict fashion is n o t faster!
> 
> And now for myself I am thrown wheter I should make the next step and
> learn Clean or I should go back to Fortran and Basic (and IDL).
> 
> I often read benchmarks that Clean is not much slower than C. And I am
> not a friend of benchmarking  into the last detail. But what the people
> do in their benchmarks? It is right that write down the code for
> calculating the fibonacci number is much more easier than in Basic or
> Fortran. But if someone needs really fibonacci numbers in a program how
> can he wait 30sec! And in that context how can someone trust in writing
> "real life applications".
> 
> I would be pleased to get answers (because I would like  printing out
> tutorials and so on, for learning Clean).
> 
> 
> Greetings,
> Siegfried Gonzi
>