Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

haskell - Function definition by special cases in GHCi

From a Haskell tutorial:

We can write functions on integers by cases.

-- Compute the sum of the integers from 1 to n.
sumtorial :: Integer -> Integer
sumtorial 0 = 0
sumtorial n = n + sumtorial (n-1)

However, here's what happens when I try it:

$ ghci
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> foo 0 = print 999
Prelude> foo n = print n
Prelude> foo 0
0

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To use those definition in GHCi exactly as they were written (i.e. with multiple equations or type signatures in separate lines), you need to use multiline input in GHCi through the :{ and :} delimiters:

GHCi> :{
GHCi| foo 0 = print 999
GHCi| foo n = print n
GHCi| :}
GHCi> foo 0
999

One alternative would be turing on multiline input for the rest of your session with the +m option. In this case, though, you also need an explicit let, as without it GHCi won't figure out you want to continue the definition:

GHCi> :set +m
GHCi> let foo 0 = print 999
GHCi|     foo n = print n
GHCi| 
GHCi> foo 0
999

(You can turn +m off with :unset +m.)

Yet another possibility is eschewing line breaks altogether, and using explicit braces and semicolons:

GHCi> foo 0 = print 999; foo n = print n
GHCi> foo 0
999

Between the multiline input options, I personally prefer :{ and :} over +m, as they require less changes with respect to how I usually phrase my definitions, and are more likely to work straight away if I paste code from somewhere else.

As for why your way of entering it didn't work, it was because, unless you use multiline input, bindings to the same name in separate GHCi lines shadow each other:

GHCi> x = 3
GHCi> x = 4
GHCi> x
4

This seems less surprising if we note that we get the same behaviour from a chain of let-expressions:

GHCi> let x = 3 in let x = 4 in x
4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...