How to switch to a new line in GHCi?

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 by pressing the enter key, but when I do this, I get:

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

It seems that the program executes the first line, but I do not want this, I want the program to allow me to write the second line and only then compile and execute

So, how can I go to the next line in Haskell (I use a terminal on Mac OS)?

+4
source share
1 answer

In ghci, you need to put the definitions on one line, and also start them with let . It is different from the source file:

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

You can also use :{ and :} to perform the definition of the muli line:

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

Make sure the second double goes back to the first. - The indentation is significant.

I recommend doing most of your work in a text editor, and then upload the file to ghci (using :load or provide it as an argument on the command line) and play with it. I don’t find ghci terribly pleasant to work with when I actually write code - it’s much better when you mess with already written code. Whenever you edit a text file,: :reload (or just :r ) in ghci.

+17
source

Source: https://habr.com/ru/post/1499168/


All Articles