I have the following function defined:
ex 1 x = 1
ex 0 x = 0
ex b x = b ** x
Then, when I execute the following:
1 `ex` (sum [1..])
it tries to calculate the sum of the infinite sequence, instead of being lazy and returning 1. Why?
EDIT: Upon further investigation, I found that laziness happens if I define the ex
function in a file, but not if I define it in GHCI:
$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude> let ex 1 x = 1
Prelude> let ex b x = b ** x
Prelude> ex 1 (sum [1..])
<interactive>: out of memory (requested 1048576 bytes)
If I pull the ex
definition into a file (in this case, test.hs):
$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude> :load test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> ex 1 (sum [1..])
1.0
The new question, then, is why?