Functional Programming 2
Functional Programming 2
Functional Programming 2
Ross Paterson
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 1 / 34
Session 1 – Introduction
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 2 / 34
Session 1 – Introduction
This session
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 3 / 34
Session 1 – Introduction Background
Bus Memory
Processor
(instructions + data)
Registers PC
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 4 / 34
Session 1 – Introduction Background
John Backus (the inventor of F ORTRAN), used this term in his 1977
ACM Turing award lecture:
“Surely there must be a less primitive way of making big
changes in the store than by pushing vast numbers of words
back and forth through the von Neumann bottleneck. Not only
is this tube a literal bottleneck for the data traffic of a problem,
but, more importantly, it is an intellectual bottleneck that has
kept us tied to word-at-a-time thinking instead of encouraging
us to think in terms of the larger conceptual units of the task
at hand. Thus programming is basically planning and detailing
the enormous traffic of words through the von Neumann bot-
tleneck, and much of that traffic concerns not significant data
itself, but where to find it.”
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 5 / 34
Session 1 – Introduction Background
Procedural programming
Problem: factorial of n = 1 ∗ . . . ∗ n
Solution: in Java:
public int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++)
fact = fact * i;
return fact;
}
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 6 / 34
Session 1 – Introduction Background
inputs output
function
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 7 / 34
Session 1 – Introduction Background
*
+ sqrt
*
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 8 / 34
Session 1 – Introduction Background
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 9 / 34
Session 1 – Introduction Background
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 10 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 12 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 13 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
More lists
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 14 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
We can ask for the type of an expression with the :type command:
interpreter
:type ’a’
:t [’a’, ’b’, ’c’]
:t "abc"
Here “a” is a type variable: it can stand for any type. These functions
are polymorphic (details in session 3).
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 15 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
Constrained polymorphism
Numeric functions also work for multiple types: interpreter
1+3
1.2+3.45
but not just anything, as we can see from its type: interpreter
:t (+)
:t mod
:t sqrt
Here Num a => is a constraint on the type. The type a must belong to
the set of types (or class) Num of numeric types, which includes
including Int, Integer and Double.
Even numeric literals can have multiple types: interpreter
:t 3
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 16 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 17 / 34
Session 1 – Introduction Haskell and the GHCi interpreter
Errors
Syntax errors interpreter
’x
2+3)*4
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 18 / 34
Session 1 – Introduction Themes
Value-oriented programming
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 19 / 34
Session 1 – Introduction Themes
What’s missing
modifiable variables
loops (but we will get to recursion later)
side effects, such as printing things from the middle of a program
breakpoints
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 20 / 34
Session 1 – Introduction Themes
Static types
Every expression in Haskell has a type:
3 has type Integer – we write 3 :: Integer, pronouncing ::
as “has type”.
2+3 :: Integer
[1,2,3] :: [Integer] (list of Integer)
product :: [Integer] -> Integer
product [1,2,3] :: Integer
(Later we will see that these have more general types.)
Other basic types: Int (fixed size integers), Double, Char, Bool.
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 21 / 34
Session 1 – Introduction Themes
Typeful programming
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 22 / 34
Session 1 – Introduction Themes
Abstraction
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 23 / 34
Session 1 – Introduction Themes
Lazy evaluation
In Haskell, expressions are not evaluated until their results are needed.
Some consequences:
We can compose functions together without feeling guilty about
large intermediate values, because they are constructed on
demand.
We can have intermediate values that are infinite (provided we
only look at part of them).
Things that must be built-in to other languages are just functions.
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 24 / 34
Session 1 – Introduction Themes
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 25 / 34
Session 1 – Introduction Themes
Values Code
1. simple functions
2. scalar values conditionals, cases
3. composite values pattern matching
4. lists list comprehensions
5. higher-order functions
6. containers
7. recursion
8. recursive datatypes
9.
10. actions do-expressions
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 26 / 34
Session 1 – Introduction Source files
GHCi commands
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 27 / 34
Session 1 – Introduction Source files
Function syntax
We can also define functions:
square :: Integer -> Integer
square n = n*n
Note: definitions go in program files; you can’t type them at the GHCi
command line.
Using a function in an expression:
f exp1 · · · expn
Type signature:
Definition:
f x1 · · · xn = exp
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 28 / 34
Session 1 – Introduction Source files
size :: Integer
size = 12+13
-- Triple an integer.
triple :: Integer -> Integer
triple n = 3*n
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 29 / 34
Session 1 – Introduction Source files
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 30 / 34
Session 1 – Introduction Source files
Haskell is case-sensitive.
Moreover, identifiers are divided into two sorts:
identifiers starting with a capital letter:
module names, like Week1.
type names, like Integer.
identifiers starting with a lower case letter:
variables, like n or function names, like square.
type variables, like a (see later).
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 31 / 34
Session 1 – Introduction Source files
Things to try
Load Week1.hs into GHCi and type: interpreter
square size
triple (square 2)
square (triple 2)
23 - triple (3+1)
23 - triple 3+1
Use the GHCi command “:type” (“:t” for short) to tell you the types
of each of these, and also triple. interpreter
:type triple
:t square size
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 32 / 34
Session 1 – Introduction Source files
Edit the file Week1.hs to add a definition of a function that triples its
input and returns the square of that:
squareOfTriple :: Integer -> Integer
squareOfTriple n = square (triple n)
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 33 / 34
Session 1 – Introduction Source files
Looking ahead
Ross Paterson (City, University of London) IN4043: Functional Programming Autumn term, 2020 34 / 34