Run and debug fast
Use ghci filename.hs
to start with a file loaded.
When in ghci, here are some useful commands:
:load file.hs
:reload
or :r
:set editor <name>
to set editor to name
:edit <name>
to edit script name
:edit
to edit current script
:show modules
:show imports
:type <expr>
to show type of expression expr
:?
for help
:quit
Conventions
- Function and arguments begins with a lower-case letter. Capital letters are for types.
- List arguments usually have an
s
suffix. E.g. xs
, ns
, nss
.
Useful functions
Haskell has library functions. Many functions are useful on lists. Imagine the lists have elements. x
represents a number.
- Select first element of list:
head []
- Remove the first element from a list:
tail []
.
- Select the nth element of a list:
[] !! x
- Select the first nth element of a list:
take x []
- Remove first n elements from a list:
drop x []
- Calculate length of list:
length []
- Calculate sum of list of numbers:
sum []
- Calculate product of a list of numbers:
product []
- Append two lists:
[] ++ []
- Reverse a list:
reverse []
Functions
Function application is denoted using space.
Multiplication is denoted using *
.
E.g. call f
with parameters a
and b
: f a b
Function calls have higher priority than any other operator.
f a + b
means (f a) + b
.
A function can be a total function or a Partial function.
Math | 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 | |
The Layout Rule
In a sequence of definitions, each definition must begin in precisely the same column.
Meaning, it works on indentation. So you don’t need to explicitly define groupings of definitions.