This is an old revision of the document!
Crash course into Haskell's syntax
Haskell in the Programming Paradigms lecture
We use Haskell at PP in order to illustrate some functional programming concepts (or functional programming design patterns) which can be used in any functional or multi-paradigm lecture. These are:
- programming with side-effect-free (or pure) functions
- programming with higher-order functions
- programming with Abstract (or Algebraic) Datatypes
- programming with lazy evaluation
With this in mind, we point out that this is not a course about the Haskell language - although we will quite a few of its particular aspects (with IO being a distinguished omission). In this page you will find the subset of programming constructs which are used during the lecture.
Functions in Haskell
Functions are defined as follows:
<function name> <param1> ... <param n> = <body expression>
For instance:
f x y = x + y
Anonymous functions can be defined as follows:
\<param1> ... <param n> -> <body expression>
For instance:
\x y -> x + y
Function calls are of the form:
<function name> <v1> ... <vn>
for a function with at least n parameters.
For instance:
f 1 2
It is a good practice (although not always necessary) to enclose a function call in parentheses, e.g. (f 1 2)
instead of f 1 2
. This actually makes
the code more legible and avoids typing bugs.
To avoid cluttering of nested function calls, e.g. f (g (h x y))
, the function application operator $
can be used. The type of $
is (a→b)→a→b
, that is, it takes a function and an argument and applies the latter on the function. The previous call can be rewritten as:
f $ g $ h x y
You can also interpret $
as a means for enforcing precedence (i.e. first h
is called, then g
then f
).
Functional composition is often a good alternative to $
. For instance, the previous function call can be written as:
(f . g) (h x y)
Meaning that function (f . g)
is called with argument (h x y)
. Note that (f . g . h) x y
is not equivalent, and signifies the call of f . g . h
with arguments x
and y
Basic datatypes
The basic datatypes used in the PP lecture are:
- integers and floats (
Integer
andFloat
) - booleans (
Bool
) - char (
Char
)
The container types are:
- lists (
[a]
) - pairs (
(a,b)
) - functions (
a → b
)
where a
and b
are type variables.