If I put the following 2 lines into foobar.hs
f 1 = 1
f x = f (x-1)
then
$ ghci
> :load foobar.hs
> f 5
1
but if I do
$ ghci
> let f 1 = 1
> let f x = f (x-1)
> f 5
^CInterrupted.
then it does not return. Why?
If I put the following 2 lines into foobar.hs
f 1 = 1
f x = f (x-1)
then
$ ghci
> :load foobar.hs
> f 5
1
but if I do
$ ghci
> let f 1 = 1
> let f x = f (x-1)
> f 5
^CInterrupted.
then it does not return. Why?
The latter binding overrides the former. Use this in ghci:
Or, without the layout:
You have to enter it all in on one line, or using
:{
and:}
to enter multiple lines:Or
When you use two
let
statements to definef
, you are actually redefiningf
the second time, not adding to its definition. If you were to doThen,
x
would be 5, not 1. The same goes for functions. First, you definef
asf 1 = 1
. Next, you definef
asf x = f (x - 1)
, which overwrites the previous definition forf
.