Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot enter multiline statements in GHCi [duplicate]

let x=1
    y=2
    z=3

does not work in GHCi, forcing me to use let {x=1;y=2;y=3} instead. How can I fix this problem?

like image 804
ThePiercingPrince Avatar asked Aug 10 '13 13:08

ThePiercingPrince


People also ask

How to define function in GHCi?

Simply type a let followed by a newline: let ⏎. Then fac 0 = 1 ⏎. Then fac n = n * fac (n-1) ⏎ ⏎ and you're done!

How to load file in GHCi?

GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.


1 Answers

The documentation says:

GHCi also has a multiline mode, enabled by :set +m, in which GHCi detects automatically when the current statement is unfinished and allows further lines to be added. A multi-line input is terminated with an empty line.

The multiline mode makes GHCi behave much like e.g. the Python interpreter:

Prelude> :set +m
Prelude> let x = 1
Prelude|     y = 2
Prelude|     z = 3
Prelude|
Prelude> (x, y, z)
(1,2,3)

This hidden gem is wonderful for playing with readable code!

If you want this to be the default behaviour, you can create a .ghci file in your home directory with a line saying :set +m. (Now that this came up, I actually did so.)

like image 131
kqr Avatar answered Oct 13 '22 08:10

kqr