2018-12-13 01:25
Page 1

Page 2

# Course content

• Functions as first-class values.
• Algebraic and abstract data types.
• Polymorphism and type 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```
• ```union        (Set xs) (Set ys) = Set (xs++[y|y<-ys,y `notElem` xs])
intersection (Set xs) (Set ys) = Set [x|x<-xs,x `elem` ys]
difference   (Set xs) (Set ys) = Set [x|x<-xs,x `notElem` ys]
```
• Fairly easy. Only finite sets. Only types in the
`Eq`
class. No
`complement`
.
• 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. Only finite sets. Only types in the
`Ord`
class. No
`complement`
.
• 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)
```
• Another example of functions as data!
• Live demo: Set.hs
Page 10
##### 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 three! All operations are one-liners!
• Allows infinite sets and
`complement`
, but no
`toList`
, unlike the others.
Page 11

Page 12

Page 13

Page 14

# 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 15

# What can Haskell be used for?

## Examples

Page 16

### From lwn.net: Stephen Diehl: Reflecting on Haskell in 2017:

• 14,000 new Haskell projects on Github!
• It’s really never been an easier and more exciting time to be programming professionally in the world’s most advanced (yet usable) statically typed language.
Page 17

# Some Haskell software I have worked on

• These slides: formatting and syntax high-lighting.
• WebFudgets, ThreepennyFudgets.
• 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 18