Download Lecture10 - CIS @ UPenn

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

Closure (computer programming) wikipedia , lookup

Currying wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Standard ML wikipedia , lookup

Anonymous function wikipedia , lookup

Combinatory logic wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Transcript
CIT 590
Intro to Programming
Functional Programming
What is a function?
• f(x) = 2x
• Very crudely put function turns inputs into outputs
• 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
Functional programming
• Functions taking other functions as an argument are
called higher order functions
• Sorting using the sorted method
Lambda functions
• Lambda x : x + 2
• Lambda x, y : x + y
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 (things you can
loop through)
• Filter – filter and only allow those that return the value true
• Reduce – a two argument function
• Go through and apply the function to pairs of elements
• First run it on 0th and 1st
• Then on 1st and 2nd
• …..
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']
n1 = filter(lambda x: len(x) > 8, names)
What is n1?
a) [‘Kevin Lee’, ‘Arvind Bhusnurmath’]
b) ‘Arvind Bhusnurmath’
c) [‘Arvind Bhusnurmath’]
d) set([‘Kevin Lee’, ‘Arvind Bhusnurmath’])
e) (‘Kevin Lee’, ‘Arvind Bhusnurmath’)
Map filter reduce on strings
• names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce
Wang']
n2 = reduce(lambda x,y : x + y[0], names)
What is n2?
a) [‘KADC’]
b) ‘KADC’
c) ‘Kevin Arvind Di Ce’
d) ‘Kevin LeeADC’
e) [‘Kevin’, ‘A’, ‘D’, ‘C’]
Map filter reduce on strings
• names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce
Wang']
n3 = map(lambda x : x.split()[-1] + x.split()[0], names)
What is n3?
a) [‘Lee Kevin’, ‘Bhusnurmath Arvind’, ‘Lu Di’, ‘Wang Ce’]
b) [‘Lee’, ‘Bhusnurmath’, ‘Lu’, ‘Wang’]
c) [‘Klee’, ‘Abhusnurmath’, ‘Dlu’, ‘Cwang’]
d) None
e) [‘LeeKevin’ ‘BhusnurmathArvind’, ‘LiDu’, ‘WangCe’]
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]
Example list comprehension
d = {'fred' : 1, 'arv': 5, 'li': 7, 'aash': 4, 'bye': 5}
What does this return?
reduce(lambda x,y : (x+y)/2 , [d[x]/2 for x in d.keys() if len(x) % 2 == 0])
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
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