0% found this document useful (0 votes)
3 views35 pages

Chapter 12b

The document provides an overview of functional programming, highlighting its paradigms, characteristics, and benefits, such as referential transparency and immutability. It discusses Haskell as a functional language, including function declaration examples and higher-order functions like map and filter. Additionally, it covers concepts like partial functions, folds, and list operations, emphasizing the importance of functions as first-class objects.

Uploaded by

teenaajith918
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

Chapter 12b

The document provides an overview of functional programming, highlighting its paradigms, characteristics, and benefits, such as referential transparency and immutability. It discusses Haskell as a functional language, including function declaration examples and higher-order functions like map and filter. Additionally, it covers concepts like partial functions, folds, and list operations, emphasizing the importance of functions as first-class objects.

Uploaded by

teenaajith918
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Functional

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)

• Create a function prod


which multiplies two
integers by each other
Pattern matching
• Your turn

• Create the function fib,


which calculates the nth
Fibonacci number.
• Test it by calling fib 30

832040
Function Composition

𝑓 3
𝑔
Integers
1 Odd Integers Boolean
F

𝑔 ∘ 𝑓 ( 𝑥)
Functional 2

Variables are ________ which means they can’t _____

Functions do not have side effects, the output depen


only on its _________
First Class Object
• An entity that can…
• Be assigned to a variable
• Assigned as arguments
• Returned by a function
Non examples
• +, -, *, /, **, //, %
# E.g. Integer • Statements (not expressions)
• break, continue
a = 5 • return
print(double(12))
def six(): Short version – there are some
things you can put in a variable
return 6 some and things you can’t
Definition
• Higher Order Function
• A function that handles
other functions as
arguments or results.
• The previous python
example
• Any python function
where you’ve used
Lambda!
A simple loop
• We want to double each item in the list [2,3,4,5,6]
• Create the list – Easy
• Create a double function – Easy
• Apply double to each item? – Hard!
• Requires a higher order function
Map
• Map is a higher order function
• It takes two arguments
• A function to apply to each
• A list
• map doub myList
Filter
• Filter is a higher order function
• It takes two arguments
• A function to check each
• A list
• Filter isEven myList
Filter Map Problems (Haskell)
1. Filter a list to remove negatives
2. Filter a list of strings to remove any more than 3
characters
• length "Hello!“ evaluates to 6, length is a function with one input of type String

3. Find all numbers 1 to 1,000,000 that meet all 3 criteria


• && is a logical and
• || is a logical or
1. Even
2. Multiples of 1,635
3. One more than a multiple of 9
What will these return?

Filter (>= 5) [2,7,6,1,9]

Filter (== 5) [2,5,7,2,5]


(rectangle_area 10) 5
Partial Functions
• A principle of functional This returns a function that
programming is that a needs one more parameter
function that takes more
than one argument can
be partially applied to a rectangle_area 10 5
smaller number of 50
arguments
rectangle_area :: Float -> Float -> Float
rectangle_area x y = x * y
rectangle_area :: Int -> (Int -> Int)
Add x y = x + y

(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!)

g :: Integer -> Integer


g (x:xs) = xs
g x = tail x (The exact same!)

h :: [Integer] -> Integer


h (x:xs) = x + h xs
h [] = 0
main = print(h [1,2,3,4])
Warm Up

Code Evaluates to...


(a) null (tail ["fish"])
(b) tail tail tail [1,2,3,4,5,6]
(c) head tail [1,2,3,4,5,6]
R :: Integer --> Integer
R x = 7 - x
(d) tail map R [1,2,3,4,5,6]
(e) map tail R [1,2,3,4,5,6]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy