A Level Computer Science
Introduction to Functional Programming
William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London
A Level Computer Science Introduction to Functional Programming - - PowerPoint PPT Presentation
A Level Computer Science Introduction to Functional Programming William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London Aims and Claims Flavour of Functional Programming . how it differs
William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London
(e.g. Python)
I hope this is convincing Only simple examples
FP Topics
Reflections
and variables
composition
Challenge problems
functional features
bigger a b = if a > b then a else b
bigger a b = if a > b then a else b bigger a b = if a > b then a else b
value plus 1
x = x + 1 # This is python Haskell has no statements
Is it possible to program without variables? Python program is a sequence
values
bigger a b = if a > b then a else b
Maths (and Haskell)
depends only on its arguments
not change anything
the same arguments always gives the same result
Python
depend on other variables
change variables
second time with the same arguments may give a different result
bigger3 a b c = bigger (bigger a b) c
double a = 2 * a square a = a * a > double (double 5) > double (square 3) > square (double 3)
circleArea r = pi * r * r circleCircum r = 2 * pi * r rectArea l h = l * h cylinderArea r h = 2 * circleArea r + rectArea (circleCircum r) h
x = x + 1 # This is python y = x * 2 x = 12
circleArea r = pi * r * r circleCircum r = 2 * pi * r rectArea l h = l * h cylinderArea r h = 2 * circleArea r + rectArea (circleCircum r) h
Python
Haskell
Functional composition ≠ sequencing of statements
def circleArea(r): return math.pi * r * r def circleCircum(r): return 2 * math.pi * r def rectArea(l, h): return l * h def cylinderArea(r, h): return 2 * circleArea(r) + \ rectArea(circleCircum(r), h)
counts the number of dots in an equilateral triangle (see picture)
trigNum 1 = 1 trigNum n = n + trigNum (n-1)
trigNum 1 = 1 trigNum n = n + trigNum (n-1) trigNum n | n == 1 = 1 | otherwise = n + trigNum (n-1)
Comparison with dry running a Python program
# Enter two marks # Save minimum mark = int(input("Mark 1 > ")) total = mark min = mark
if mark < min: min = mark total = total + mark
average = total / 2
if min < 30 or average < 50: grade = "fail" else: grade = "pass"
Step Variable mark total min average grade 1 35 2 35 3 35 4 45 5 80 6 40 7 fail
trigNum 1 = 1 trigNum n = n + trigNum (n-1)
trigNum 3 = 3 + trigNum 2 = 3 + 2 + trigNum 1 = 3 + 2 + 1 = 6
1 : 2 : 3 : []
Function Description Example elem Member of list Main> elem 4 [1,2,3,4,5] True Main> elem 4 [1,3,5] False head First element of list Main> head [2,4,6,8] 2 tail List without first element Main> tail [3,5,7,9] [5,7,9] ++ Concatenate two lists Main> [1,2,3] ++ [7,9] [1,2,3,7,9]
[1 .. 10]
len [] = 0 len (x:xs) = 1 + len xs
How to do without loops
Python
Haskell
Iteration & recursion equally expressive
forLoop 0 _ x = x forLoop n f x = forLoop (n-1) f (f n x) sumup n = forLoop n (+) 0
Control value Result so far Function in loop
processing a list
element
addOne [] = [] addOne (x:xs) = x+1:addOne xs
square [] = [] square (x:xs) = x*x:square xs
a list
inc x = x + 1
addOne ls = map inc ls square x = x * x
squares xs = map square xs Function as argument. Map is ‘higher-order’
map f [] = [] map f x:xs = f x : map f xs map inc [1,2,3] = inc 1 : map inc [2,3] = inc 1 : inc 2 : map inc [3] = inc 1 : inc 2 : inc 3 : map inc [] = inc 1 : inc 2 : inc 3 : [] = [2, 3, 4]
len [] = 0 len (x:xs) = 1 + len xs
addUp [] = 0 addUp (x:xs) = x + addUp xs
count x y = y + 1
len xs = foldr count 0 xs add x y = x + y
addUp xs = foldr add 0 xs
foldr f a [] = a foldr f a x:xs = f x (foldr f a xs) foldr add 0 [1,2,3] = add 1 (foldr add 0 [2,3]) = add 1 (add 2 (foldr add 0 [3])) = add 1 (add 2 (add 3 (foldr add 0 []))) = add 1 (add 2 (add 3 0)) = add 1 (add 2 3) = add 1 5 = 6
moreThan a b = b > a Main> filter (moreThan 3) [3,2,5,1,7,8] [5,7,8]
the other two Function Description map Apply function to each list element filter Select elements satisfying a predicate foldr Combine elements using a function Often called reduce
computers) map and reduce (fold) available in many languages
Machine User
C Java Haskell
… and teaching FP
We Have Covered
expressions
variables
.. More Ideas
lambda
Is using FP to reflect on Imperative programming useful?