Function definition by special cases in GHCi

后端 未结 2 1690
我在风中等你
我在风中等你 2020-11-28 16:12

From a Haskell tutorial:

We can write functions on integers by cases.

-- Compute the sum of         


        
2条回答
  •  半阙折子戏
    2020-11-28 17:10

    ghci is an interactive tool and as such allows to redefine a function when it's already be define. In your case, it doesn't see it as a two lines function definition but as two attempts of defining it. So f n = print n overrides f 0 = print 999 instead of completing it.

    To enter multiple lines statement in ghci there is a special syntax. You need to do

    Prelude> :{
    Prelude> let foo 0 = print 999
    Prelude>     foo n = print n
    Prelude> :}
    

提交回复
热议问题