Functional Programming Slides
Functional Programming Slides
Functional Programming
Ralf Hinze
Universität Kaiserslautern
April 2019
Part 0
Overview
0.0 Outline
Aims
Motivation
Organization
Contents
Literature
0.1 Aims
0.2 Motivation
LISP is worth learning [because of] the profound
enlightenment experience you will have when you finally
get it. That experience will make you a better programmer
for the rest of your days, even if you never actually use
LISP itself a lot.
0.2 FP in industry
• Website: https://pl.cs.uni-kl.de/fp19
• your goal: obtain a good grade
• (my goal: get you interested in FP)
• how to achieve your goal:
ñ make good use of me i.e. attend lectures
ñ make good use of my teaching assistant: Sebastian Schweizer
ñ obtain at least a sufficient grade for 75% of the exercises
ñ work and submit in groups of 3–4
ñ submission: Tuesday 12:00 noon
ñ exercise session: Thursday, 11:45 - 13:15, Room 48-453
ñ pass the final exam
• a gentle request and a suggestion:
keep the use of electronic devices to a minimum;
make notes using pencil and paper
z : = (2 ∗ a ∗ y + b ) ∗ (2 ∗ a ∗ y + c )
= t := 2 ∗ a ∗ y ; z := (t + b) ∗ (t + c)
z : = (2 ∗ a ∗ y + b ) ∗ (2 ∗ a ∗ y + c )
ac : = 2 ; ac ∗= a ; ac ∗= y ; ac += b ; t : = ac ;
ac : = 2 ; ac ∗= a ; ac ∗= y ; ac += c ; ac ∗= t ;
z : = ac
• insertion sort
• quicksort
• binary search trees
insertionSort [ ] = []
insertionSort (x : xs) = insert x (insertionSort xs)
insert a [ ] = [a]
insert a (b : xs)
|aàb = a : b : xs
| otherwise = b : insert a xs
0.5 Quicksort: C
quickSort [ ] = []
quickSort (x : xs) = quickSort littles ++ [x] ++ quickSort bigs
where littles = [a | a ← xs, a < x]
bigs = [a | a ← xs, x à a]
data Tree elem = Empty | Node (Tree elem) elem (Tree elem)
inorder Empty = []
inorder (Node l a r) = inorder l ++ [a] ++ inorder r
insert k Empty = Node Empty k Empty
insert k (Node l a r)
|kàa = Node (insert k l) a r
| otherwise = Node l a (insert k r)
0.6 Literature
Part 1
1.0 Outline
Evaluation
Functions
Definitions
Summary
1.1 Calculators
smaller (3, 4) = 3
1.1 Layout
• elegant and unobtrusive syntax
• structure obtained by layout, not punctuation
• all definitions in same scope must start in the same column
• indentation from start of definition implies continuation
1.1 A session
iiii 42
42
iiii 6 ∗ 7
42
iiii square 7 − smaller (3, 4) − square (smaller (2, 3))
42
iiii square 1234567890
1524157875019052100
expr1
=⇒ { why? }
expr2
=⇒ { why? }
expr3
1.2 Evaluation
• interpreter evaluates expression by reducing to simplest
possible form
• reduction is rewriting using meaning-preserving
simplifications: replacing equals by equals
square (3 + 4)
=⇒ { definition of ‘+’ }
square 7
=⇒ { definition of square }
7∗7
=⇒ { definition of ‘∗’ }
49
square (3 + 4)
=⇒ { definition of square }
(3 + 4) ∗ (3 + 4)
=⇒ { definition of ‘+’ }
7 ∗ (3 + 4)
=⇒ { definition of ‘+’ }
7∗7
=⇒ { definition of ‘∗’ }
49
1.2 Values
1.3 Functions
(\x → x + x) 47 =⇒ x + x {x := 47} =⇒ 47 + 47 =⇒ 94
1.3 Operators
1.3 Associativity
• why operators at all? why not prefix notation?
• there is a problem of ambiguity:
x⊗y⊗z
(x + y) + z = x + (y + z)
x+0=x=0+x
1.3 Association
1.3 Precedence
x⊕y⊗z
infixl 7 ∗
infixl 6 +
1.3 Composition
(g ◦ f) x = g (f x)
spread f g x = (f x) (g x)
kill a x=a
• we calculate
1.4 Definitions
name :: String
name = "Ralf"
1.4 let-expressions
program
model
problem expression
solve evaluate
solution value
interpret
Part 2
2.0 Outline
Strong typing
Functions
Tuples
Parametric polymorphism
Type synonyms
Type classes
Summary
• Booleans
• characters
• strings
• numbers
2.2 Booleans
• type Bool (note: type names capitalized)
• two constants, True and False (note: constructor names
capitalized)
• e.g. definition by pattern-matching
f False = ...
f False = ...
f True = ...
2.2 Characters
• type Char
• constants in single quotes: ’a’, ’?’
• special characters escaped: ’\’’, ’\n’, ’\\’
• ASCII coding: ord :: Char → Int, chr :: Int → Char (defined in
library module Data.Char)
• comparison and ordering, as before
2.2 Strings
• type String
• (actually defined in terms of Char, see later)
• constants in double quotes: "Hello"
• comparison and (lexicographic) ordering
• concatenation +
+
• display function show :: Integer → String (actually more
general than this; see later)
2.2 Numbers
2.2 Enumerations
• e.g. Bool is not built in (although if ... then ... else syntax is):
2.3 Functions
2.4 Tuples
fst (x, ) = x
fst :: (a, b) → a
f :: T → U
h :: (((Int → a) → a) → b) → b
h = \f → f (\g → g 0)
• cannot be recursive
• just a ‘macro’: no new type
• Eq: ,6
• Ord: < etc, min etc
• Enum: succ, . .
• Bounded: minBound, maxBound
• Show: show :: a → String
• Read: read :: String → a
• Num: +, ∗ etc
• Real (ordered numeric types)
• Integral: div etc
• Fractional: / etc
• Floating: exp etc
• more later
2.8 Summary