I'm a beginner in Haskell, and I tried entering the following in WinGHCi:
Prelude> factorial 0=1
Prelude> factorial n=n*factorial (n-1)
Prelude> factorial 5
But when I did this, WinGHCi got stuck and didn't do anything. Why didn't it print out the factorial of 5?
When you write in GHCi
> let x = 4
> let x = 5
the second definition overrides the first one, removing it from the environment. This also holds for functions.
> let f 0 = 1
> let f n = 1 + f (n-1)
is equivalent to
> let f n = 1 + f (n-1)
which will recurse forever on any input.
In GHCi, you can have both using
> let f 0 = 1 ; f n = 1 + f (n-1)
but the best approach is to edit a .hs
file, write your definitions there, and then load it in GHCi.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.