Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

haskell - How to define a function in ghci across multiple lines?

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?

Question&Answers:os

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...