Chapter 12b
Chapter 12b
Programming
Functional 1
Starter
• Will/should these function calls always do the same
thing?
Programming Paradigms
• Procedural
• Object Oriented
• Functional
• Declarative
Functional Languages
• Has no side effects (Referential Transparency)
• Haskell Python Strings are
immutable
• Data structures are immutable (can’t change)
• Variables are not really a thing
• Benefits
• Supporting the decomposition and abstraction of computing
problems into functions that are made up of other functions
that are made up of other functions, and so on
• Being suitable for running parallel and concurrent program
execution, which allows functional programs to run on
multiprocessor systems
Why!?
• It is high level
• Facebook uses it (Sigma)
• Every interaction gets checked
• Policies can’t interact
• Concurrency
• Updates quickly
• Runs fast
Haskell
• Variables • Functions
• Case sensitive • No brackets
• If it has 3 arguments, it
• A = 10 expects 3 values
• A = 15 --error • Function call
• More of a constant • round 3.7
• Evaluates to 4
• Data types • add 1 2 3
• Int • Evaluates to 6
• Float • Loops? Nope!
• Bool • IF/Else
• Char • Can be used – not in spec
Declaring functions (Easy Level)
• In Maths… • In Haskell
• f x = 2*x + 1
• In Python • Function name f
• Parameter x
• Output calculation 2*x+1
Declaring functions (Medium Level)
• In Maths… • In Haskell
• f :: Integer -> Integer
• f x = 2*x + 1
• In Python
• Function name f
• Parameter x
• Output calculation 2*x+1
• Haskell data types include
Int, Float, Bool, Char.
Declaring functions (Hard Level)
• In Maths… • In Haskell
• f :: Integer -> Integer
-> Float
• Called Cartesian Product
• f x y = (x+y)/2
• In Python
Your Turn
• Create the function Avg
• Create a function five that that calculates the
multiples an integer by 5 average of 3 numbers
(floats)
832040
Function Composition
𝑓 3
𝑔
Integers
1 Odd Integers Boolean
F
𝑔 ∘ 𝑓 ( 𝑥)
Functional 2
(Add 5) 4
Add 5
Returns a
Give 4 to the
Partial
partial function
function
to complete it
SumFour :: Int -> (Int -> (Int -> (Int -> Int))))
SumFour a b c d = a + b + c + d
SumFour 1 2 3 4
SumThree = SumFour 0
(((SumFour 1) 2) 3) 4
((Partial Function 2) 3) 4
(Partial Function 3) 4
Partial Function 4
Functional 3
Applying 4 to add gives a new function that still requires one more input. When 6 is given to this partially applied function, it is evaluated to produce the final result.
Starter
• What is a higher add x y = x + y
order function? [2] add :: Int -> Int -> Int
• Describe how the add function
• A higher-order could be partially applied to the
function is a arguments 4 and then 6. [2]
function that takes
another function • Applying 4 to add gives a new
as an argument or function that still requires one
returns a function input.
as a result. • When 6 is given to the new partial
function, it is evaluated.
add3Integers :: integer (integer (integer integer)
Fold
• Higher order function
• Reduces a list to a single value recursivly
• fold (+) 0 [1,2,3]
• function, starting value, list
• For Haskell we use foldl
• Fold starting at the Left end of the list
Lists
• Made of a head and a tail
• The head is a single element
• The tail is also it’s own list
• [ ] is the empty list
What might this do?
Other List Functions
• null list • length list
• Boolean is it empty • Integer number of elements
Heads & Tails
f :: Integer -> Integer
f (x:xs) = x
f x = head x (The exact same!)