[Haskell-beginners] why isnt this optimized?

Gunnar Quehl hasquehl at gmx.de
Fri Sep 16 02:24:18 UTC 2016


Hi,

I wrote a recursive fibonacci function in ghci:

:set +m

let
f 0 = 0
f 1 = 1
f n = f (n-1) + f (n-2)

As expected calculation f 30 takes a while. About 5s on my machine.
What I don't understand is that

let x = f 30 in (x,x)

takes twice as long. Why is ghci reevaluating the x twice? Isn't in 
always said,
that it can optimize because we can replace same by same and so on.

Actually if compiled the behaviour is as expected.

main = print $ let x = f 34 in (x)

and

main = print  $ let x = f 34 in (x,x)

both take about 3s.

Why is this not the case in the interactive enviroment?

Thanks
Gunnar



More information about the Beginners mailing list