Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to a new line in GHCi? [duplicate]

Tags:

haskell

I want to write a function, something like this

double :: Int -> Int
double x = x + x

The problem is that after I write the first line:

Prelude> double :: Int -> Int

I try to go to the next line pressing the enter key, but when I do I get:

<interactive>:84:1: Not in scope: `double'
Prelude>

It seems that the program executes the first line, but I dont want that, I want the program to let me write the second line and only then compile and execute

So, how can I go to the next line in Haskell (Im using the Terminal on Mac OS)?

like image 996
haskelllearner Avatar asked Aug 27 '13 15:08

haskelllearner


People also ask

How do you enter a new line in Haskell?

To create a string containing a newline, just write "\n" .

How do you write multiple lines in GHCi?

Note the vertical bars on the left are for GHCi multiple lines. If you use :{ and :} you don't need to specify let before your type declaration, meaning that you don't need to indent second and subsequent lines.

How do I get out of GHCi REPL?

Quits GHCi. You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed.

How does GHCi work?

GHCi will discover which modules are required, directly or indirectly, by the topmost module, and load them all in dependency order. If you started up GHCi from the command line then GHCi's current directory is the same as the current directory of the shell from which it was started.


1 Answers

In ghci, you have to put definitions on a single line, and also begin them with let (EDIT: you don't have to start ghci definitions with let anymore). It's different than in a source file:

ghci> let double :: Int -> Int; double x = x + x

You can also use :{ and :} to do a muli-line definition:

ghci> :{
Prelude| let double :: Int -> Int
Prelude|     double x = x + x
Prelude| :}
ghci> double 21
42

Make sure to indent the second double to line up with the first one -- indentation is significant.

I recommend doing most of your work in a text editor, and then load the file into ghci (with :load, or providing it as an argument on the command line) and playing with it. I don't find ghci terribly pleasant to work with when actually writing code -- it's much better at messing around with code that's already written. Whenever you modify the text file, :reload (or just :r) in ghci.

like image 51
luqui Avatar answered Oct 28 '22 12:10

luqui