How to define a function in ghci across multiple lines?

Peter McG picture Peter McG · May 17, 2010 · Viewed 69.2k times · Source

I'm trying to define any simple function that spans multiple lines in ghci, take the following as an example:

let abs n | n >= 0 = n
          | otherwise = -n

So far I've tried pressing Enter after the first line:

Prelude> let abs n | n >= 0 = n
Prelude>           | otherwise = -n
<interactive>:1:0: parse error on input `|'

I've also attempted to use the :{ and :} commands but I don't get far:

Prelude> :{
unknown command ':{'
use :? for help.

I'm using GHC Interactive version 6.6 for Haskell 98 on Linux, what am I missing?

Answer

karakfa picture karakfa · Nov 5, 2013

GHCi now has a multiline-input mode, enabled with :set +m. For example,

Prelude> :set +m
Prelude> let fac 0 = 1
Prelude|     fac n = n * fac (n-1)
Prelude|
Prelude> fac 10
3628800