It works when loaded from file, but not when typed into ghci. Why?

后端 未结 2 773
小蘑菇
小蘑菇 2020-12-11 17:56

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
         


        
2条回答
  •  旧巷少年郎
    2020-12-11 18:38

    The latter binding overrides the former. Use this in ghci:

    Prelude> :{
    Prelude| let f 1 = 1
    Prelude|     f x = f (x-1)
    Prelude| :}
    Prelude> f 5
    1
    

    Or, without the layout:

    Prelude> let f 1 = 1; f x = f (x-1)
    Prelude> f 5
    1
    

提交回复
热议问题