1

I'm on my Mac trying out Haskell and to my surprise, when I defined a function, I got an error:

Prelude System.IO> :set prompt "ghci> "
ghci> addMe :: Int -> Int -> Int

<interactive>:11:1: error:
    Variable not in scope: addMe :: Int -> Int -> Int
ghci>

How can I define functions on the ghci?

joesan
  • 13,963
  • 27
  • 95
  • 232
  • Use multiline input or explicit semicolons, as demonstrated in [*Function definiton by special cases in GHCi*](http://stackoverflow.com/q/42593284/2751851). – duplode Apr 13 '17 at 19:10
  • 3
    I find it easier to put all definitions in a file I then `:load` into ghci. – melpomene Apr 13 '17 at 19:10
  • Consider also [IHaskell](https://github.com/gibiansky/IHaskell), which combines the direct-feedback experience of typing directly in the interpreter with the structure and backup capabilities of a proper source file. – leftaroundabout Apr 13 '17 at 19:20

1 Answers1

1

If you want to add a type signature to a definition in GHCi, you can specify it by using multiline input through :set +m or :{ ... :}, as described in the GHC Users Guide, or by using semicolons, as in:

mulme x y = [x*y| x /= 0, y/= 0]; mulme :: Int -> Int -> [Int]
duplode
  • 33,731
  • 7
  • 79
  • 150
Aerron
  • 310
  • 1
  • 12
  • Note that we generally use `Maybe` instead of lists that will always have at most one element. By doing it that way, the right-hand side of the definition above might become `if (x /= 0 && y /= 0) then Just (x * y) else Nothing`. – duplode Apr 15 '17 at 00:55