Learning goals

  • To be able to explain the notion of a function in the functional programming paradigm in a precise way
  • To understand the central points in the history of functional programming
  • To be able to install the Glasgow Haskell compiler and use it with a simple programming environment
  • To be able to write simple definitions in Haskell
  • To be able to edit, load and use simple Haskell scripts
  • To understand and be able to apply simple aspects of Haskell syntax: Definitions, comments, the layout rule and where declarations in definitions

Haskell

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.

MathHaskell
$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.

Outgoing links: Haskell, Partial function