GHCiで複数行の関数を定義

GHCiでパターンマッチのような複数行の関数を定義しようとすると後で定義した行のみが有効になってしまう。


Prelude> let fact 0 = 1
Prelude> let fact n = n * fact (n-1)
Prelude> fact 10
*** Exception: stack overflow
Prelude> :t fact
fact :: (Num a) => a -> a

次のようにするとおk。


Prelude> :{
Prelude| let { fact 0 = 1
Prelude| ; fact n = n * fact (n-1)
Prelude| }
Prelude| :}
Prelude> fact 10
3628800
Prelude> :show bindings
fact :: (Num t) => t -> t = _
it :: Integer = 1

栄光のグラスゴーHaskellコンパイルシステム利用の手引き バージョン6.10.2

3.4.2. プロンプトでdo記法を使う