AnchovyLegend
AnchovyLegend

Reputation: 12537

Haskell functions in GHCi

I am completely new to Haskell. I have been trying to learn how to write functions, lets say to add two integer numbers. I am currently using GHCi to code Haskell. I tried learning from http://www.haskell.org/tutorial/functions.html, however this does not work, I get a not in scope error. I greatly appreciate any help with this. Am I supposed to not use GHCi to code haskell in order to create functions? GHCi, seems to work okay so far, for everything other than functions.

Many thanks in advance.

Upvotes: 2

Views: 899

Answers (1)

Adam Gordon Bell
Adam Gordon Bell

Reputation: 3093

You have to use let to declare functions in GHCI

ghci>let add x y = x + y
ghci>add 3 3
6

In general though I would advice you to open up a text editor and write your functions in there, save as .hs and open it with :l in ghci

Like this ( from RWH)

-- file: ch03/add.hs
add a b = a + b

Then:

ghci> :l add.hs
[1 of 1] Compiling Main             ( add.hs, interpreted )
Ok, modules loaded: Main.
ghci> add 1 2
3

Upvotes: 3

Related Questions