11

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)?

T I
  • 9,785
  • 4
  • 29
  • 51
haskelllearner
  • 189
  • 2
  • 8
  • 1
    With ghci, a better way to write haskell code is to edit a file and use :load and :reload. This way you avoid retyping after every mistake. – seanmcl Aug 27 '13 at 15:08

1 Answers1

36

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.

luqui
  • 59,485
  • 12
  • 145
  • 204