The interpreter can be started from the terminal command prompt $ by simply typing ghci
$ ghci
GHCi, version X: http://www.haskell.org/ghc/
:? for help
Prelude>
The GHCi prompt > means that the interpreter is now ready to evaluate an expression.
For example, it can be used as a desktop calculator to evaluate simple numeric expresions:
> 2+3*4
14
> (2+3)*4
20
> sqrt (3^2 + 4^2)
5.0
2+3*4
(2+3)*4
sqrt (3^2 + 4^2)
Haskell comes with a large number of standard library functions. In addition to the familiar numeric functions such as + and *, the library also provides many useful functions on lists.
head [1,2,3,4,5]
tail [1,2,3,4,5]
[1,2,3,4,5] !! 2
take 3 [1,2,3,4,5]
drop 3 [1,2,3,4,5]
length [1,2,3,4,5]
sum [1,2,3,4,5]
product [1,2,3,4,5]
[1,2,3] ++ [4,5]
reverse [1,2,3,4,5]
In mathematics, function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space.
$$ f(a,b) + c d $$Apply the function f to a and b, and add the result to the product of c and d.
In Haskell, function application is denoted using space, and multiplication is denoted using *.
f a b + c*d
As previously, but in Haskell syntax.
Moreover, function application is assumed to have higher priority than all other operators.
f a + b
Means
(f a) + b
, rather thanf (a + b)
.
Examples
Mathematics | Haskell |
---|---|
$f(x)$ | f x |
$f(x, y)$ | f x y |
$f(g(x))$ | f (g x) |
$f(x, g(y))$ | f x (g y) |
$f(x) g(y)$ | f x * g y |
When developing a Haskell script, it is useful to keep two windows open, one running an editor for the script, and the other running GHCi.
Start an editor, type in the following two function definitions, and save the script as test.hs:
double x = x + x
quadruple x = double (double x)
Leaving the editor open, in another window start up GHCi with the new script:
ghci test.hs
Now both the standard library and the file test.hs are loaded, and functions from both can be used:
> quadruple 10
40
> take (double 2) [1,2,3,4,5,6]
[1,2,3,4]
Leaving GHCi open, return to the editor, add the following two definitions, and resave:
factorial n = product [1..n]
average ns = sum ns `div` length ns
Note:
- div is enclose in back quotes, not forward;
- x 'f' y is just syntatctic sugar for f x y
GHCi does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used:
> :reload
Reading file "test.hs"
> factorial 10
3628800
> average [1,2,3,4,5]
3
Command | Meaning |
---|---|
:load name | load script name |
:reload | reload current script |
:set editor name | set editor to name |
: edit name | edit script name |
:edit | edit current script |
: type expr | show type of expr |
:? | show all commands |
:quit | quit GHCi |
myFun fun1 arg_2 x’
xs ns nss
In a sequence of definition, each definition must begin in precisely the same column:
Correct
a = 10
b = 20
c = 30
Incorrect
a = 10
b = 20
c = 30
Incorrect
a = 10
b = 20
c = 30
The layout rule avoids the need for explicit syntax to indicate the grouping of definitions.
The following snippets are the same
-- Implicit grouping
a = b + c
where
b = 1
c = 1
d = a * 2
-- explicit grouping
a = b + c
where
{b = 1;
c = 1}
d = a * 2
N = a 'div' length xs
where
a = 10
xs = [1,2,3,4,5]