Download Lecture 10

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Tail call wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Currying wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Anonymous function wikipedia , lookup

Standard ML wikipedia , lookup

Combinatory logic wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda lifting wikipedia , lookup

Transcript
CIT 590
Intro to Programming
Lecture 10
Agenda
• Functional programming
• Functional programming as it is done in Python
The three things I will never give an
assignment on ….
• Religion
• Politics
and ….
• Harry Potter!
• https://canvas.upenn.edu/courses/1164397/announcemen
ts
What is a function?
• f(x) = 2x
• A function works on sets
• What is a set? .. Is it like lists??
• Can I think of a function the way that I would think of lists
and dictionaries etc
• Can I pass a function as an argument to another function
• YES!
• The first time you see this, it looks crazy
• Just start thinking of a function as a machine that turns
something into something else
Is functional programming actually used?
• YES YES YES
• C# uses it like you wouldn’t believe. Even more so if you
use LINQ
• Scala, being taught in 591 uses it a lot!
• It offers way more flexibility
• Do not dismiss it the way I once did
Functional programming
• Functions taking other functions as an argument are
called higher order functions
• We have already seen an example of this – sort
• Check out example of complex number sorting
Lambda functions
• Lambda x : x + 2
• Lambda x, y : x + y
In some ways, I think of lambda’s as ‘I am lazy and I do not
want to write a function so let us do this instead’.
Map, filter and reduce
Python provides this as
• Map – transform everything in the iterable
• Filter – filter those that return the value true
• Reduce – a two argument function
ls1 = map(lambda x: x +2, a)
ls2 = filter(lambda x: x%2 == 0, a)
ls3 = reduce(lambda x, y : x+y, a )
Map filter reduce on strings
• names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce
Wang']
• Use map to convert this list into the lastname, firstname
format
• Use filter to remove all names longer than 11 characters
• Use reduce to find the longest string
List comprehensions
• [expr for var in list if expr]
• Basically a cool way of combining the map and filter
functions
• [x*2 for x in a if x<2]
• These, thankfully, do not work by side effect
Lst = [x*2 for x in a if x < 2]
• d = {1: ‘fred’, 2:’arv’, 3:’li’}
[d[i] for i in d.keys() if i % 2 == 0]
Comparison of imperative and functional
programming version (taken from msdn)
Characteristic
Imperative approach
Functional approach
Programmer focus
How to perform tasks
(algorithms) and how to
track changes in state.
What information is
desired and what
transformations are
required.
State changes
Important.
Non-existent.
Order of execution
Important.
Low importance.
Primary flow control
Loops, conditionals, and
function (method) calls.
Function calls, including
recursion.
Primary manipulation unit
Instances of structures or
classes.
Functions as first-class
objects and data
collections.
Sieve example
• Functional programming lends itself to recursion really
•
•
•
•
•
easily
Erasthothenes algorithm for getting rid of composite
numbers in a list of increasing numbers beginning with the
number 2
An ancient algorithm for finding prime numbers
From any list of increasing numbers remove the multiples
of any number in the list
Easy for list of size 1
Can I do this recursively???
The 3 argument version of the reduce
function
• We have already seem reduce(function, list)
• There is a 3 argument variant which is
• Reduce(function, list, identity element/first element for the process
• Sorting examples
• Define insertion of an element into a sorted list
• Now use a reduce operation
Quicksort
• List comprehensions at their badass best!
• Quicksort.py
Common super useful reductions
• Reduce(lambda x,y : x+y ,a)
• Sigma/summation
• Reduce(lambda x,y: x*y , a, 1)
• product
• Reduce(lambda x,y: x + 1, a, 0)
• Num elements
• Reduce(lambda x,y: [y] + x, a, [])
• reverse
• the usage of reductions when dealing with lists