Page 1

Page 2

# Course content

• Functions as first-class values.
• Algebraic and abstract data types.
• Polymorphism and classes.
• Testing functional programs.
• Lazy evaluations and infinite objects.
Page 3

# Learning outcomes

• Write small to medium-sized functional programs for a variety of applications.
• Exploit a variety of programming techniques typical in functional programming, such as:
• Use of recursion,
• Modelling with recursive datatypes,
• Abstraction and reuse with the help of higher order functions and monads.
• Appreciate the strengths and possible weaknesses of the functional programming paradigm.
Page 4

# Functions as first class values

• This is a key aspect of functional programming.
• Higher order functions
• Powerful way to build modular and reusable code.
• Especially when combined with polymorphism.
• Using functions to represent data
Page 5

# Example: an abstract data type for sets

• ```data Set a

empty        :: Set a
singleton    :: a -> Set a
insert       :: a -> Set a -> Set a

union, intersection, difference
:: Set a -> Set a -> Set a

complement   :: Set a -> Set a

member       :: a -> Set a -> Bool

toList       :: Set a -> [a]
```
• Probably need some constraints,
`Eq a`
or
`Ord a`
, depending on implementation.
Page 6

# How to represent sets

## Three possible representations

• Lists
• ```data Set a = Set [a]
```
• Binary search trees
• ```data Set a = Empty | Node a (Set a) (Set a)
```
• Functions
• ```data Set a = Set (a->Bool)
```
• Which is easier?
Page 7

# Representing sets as lists

• ```data Set a = Set [a] -- Invariant: no duplicates

empty = Set []
singleton x = Set [x]

insert x (Set xs) | x `elem` xs = Set xs
| otherwise   = Set (x:xs)

member x (Set xs) = x `elem` xs```
• Fairly easy.
• Variant: keep the lists ordered for efficiency.
Page 8

# Representing sets as binary search trees

• ```data Set a = Empty | Node a (Set a) (Set a)
-- Invariant: smaller elements to the left,
--            bigger to the right
empty = Empty
singleton x = Node x Empty Empty

insert x Empty = singleton x
insert x (Node y l r) | x==y = Node y l r
| x<y  = Node y (insert x l) r
| x>y  = Node y l (insert x y)

member x Empty        = False
member x (Node y l r) = x==y ||
member x (if x<y then l else r)
```
• More complicated.
• Even more complicated if we want to keep the trees balanced. (Data.Set)
Page 9

# Representing sets as functions

• ```data Set a = Set (a->Bool)

empty = Set (const False)
singleton x = Set (==x)
insert x s = union (singleton x) s

member x (Set f) = f x```
• ```union        (Set f) (Set g) = Set (\x->f x || g x)
intersection (Set f) (Set g) = Set (\x->f x && g x)
complement   (Set f)         = Set (not . f)

difference s1 s2 = intersection s1 (complement s2)
```
• It's the easiest of the tree! All operations are one-liners!
• Can implement
`complement`
, but not
`toList`
, unlike the others.
Page 10

Page 11

Page 12

Page 13

# Software prototyping experiment (4)

## Key design choice

• ```type Region = Point -> Bool

type Point = (Double,Double)
```
• This makes all operations on regions easy to define
• Basic shapes: circles, rectangles, etc
• Geometric transformations, e.g. moving, scaling & rotating regions
• Unions, intersections, complements
• Membership tests
Page 14

# Loose Ends

• QuickCheck
• Using
`collect`
to examine test data distribution.
• Testing functions with preconditions (e.g.
`insert`
).
• Informative error messages from the monadic evaluator
• Laziness
• The fringe eguality example.
• Lazy IO.
Page 15

# What can Haskell be used for?

## Examples

• GHC is implemented in Haskell.
• Hackage: lots of free Haskell libraries and applications.
• Investment banking: financial modelling, quantitative analysis.
• Keera Studios: game development, Android.
Page 16

# Some Haskell software I have worked on

• These slides: formatting and syntax high-lighting.
• Web Fudgets: Othello.
• Programatica: Haskell compiler front-end (2001-2006)
• House: a prototype operating system in Haskell (2004-2006)
• Hardware emulation (6502 8-bit processor, used in C-64)
• An e-commerce system in Haskell (2006-2009)
• A web browser in Haskell (mid 1990s)
• Alfa: GUI for the proof assisant Agda (mid 1990s)
• Fudgets: GUI library in Haskell (early 1990s)
Page 17