Top Banner
CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012
40

CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

Dec 17, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

CS61A Lecture 4Higher Order Functions

Tom Magrino and Jon KotkerUC Berkeley EECS

June 21, 2012

Page 2: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

2

COMPUTER SCIENCE IN THE NEWS

http://blogs.wsj.com/tech-europe/2012/06/20/ai-system-automates-consumer-complaints/

Page 3: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

3

Write the function filtered_count that takes a number, n, and a function, pred*, and prints all the numbers 1 to n for which pred returns True.

def filtered_count(n, pred): k = 1 while k <= n: if pred(k): print(k) k = k + 1

REVIEW

*pred stands for predicate, a function that only returns True or False.

Page 4: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

4

Write the function filtered_count that takes a number, n, and a function, pred*, and prints all the numbers 1 to n for which pred returns True.

def filtered_count(n, pred): k = 1 while k <= n: if pred(k): print(k) k = k + 1

REVIEW

*pred stands for predicate, a function that only returns True or False.

Page 5: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

5

TODAY

Defining functions inside functions.Returning functions from functions.A Friendly Greek Letter.

http://gingerconsult.files.wordpress.com/2012/06/turn-it-up.png?w=300&h=300

Page 6: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

6

LOCAL (HELPER) FUNCTIONS

Python allows us to make functions inside other functions. This is useful for making helper

functions.

def funny_function(a, b, c): a_funny_b = (a * b) + (b / a) + (a ** b) a_funny_c = (a * c) + (c / a) + (a ** c) return a_funny_b / a_funny_c

Page 7: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

7

LOCAL (HELPER) FUNCTIONS

Python allows us to make functions inside other functions. This is useful for making helper

functions.

def funny_function(a, b, c): def a_funny(b): return (a * b) + (b / a) + (a ** b) return a_funny(b) / a_funny(c)

Locally defined “helper” functions help us avoid repetition!

Page 8: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

8

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(a, b, c): def a_funny(b): return (a * b) + (b / a) + (a ** b) return a_funny(b) / a_funny(c)

funny_function(2, 4, 6)

Page 9: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

9

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(a, b, c): def a_funny(b): return (a * b) + (b / a) + (a ** b) return a_funny(b) / a_funny(c)

funny_function(2, 4, 6)

Page 10: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

10

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, b, c): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(b) / a_funny(c)

funny_function(2, 4, 6)

Page 11: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

11

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, b, c): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(b) / a_funny(c)

funny_function(2, 4, 6)

Page 12: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

12

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

Page 13: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

13

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

Page 14: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

14

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

Page 15: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

15

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(4): return (2 * 4) + (4 / 2) + (2 ** 4) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

26

Page 16: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

16

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

26

Page 17: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

17

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(6): return (2 * 6) + (6 / 2) + (2 ** 6) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

26 79

Page 18: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

18

LOCAL (HELPER) FUNCTIONS

How does this work?

def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6)

funny_function(2, 4, 6)

26 79

0.329…

Page 19: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

19

ANNOUNCEMENTS

• You will be placed into study/exam groups in discussion section today.– Email your TA ASAP if you can’t make section so

they can assign you a group.• Homework 1 is due tomorrow night.• Homework 2 is out and due Tuesday night.• Project 1 is out and due the night of 6/29.

Page 20: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

20

PROBLEM: MAKING ADDERSdef add_1_to_twice(n): return (2*n)+1def add_2_to_twice(n): return (2*n)+2def add_3_to_twice(n): return (2*n)+3...def add_65536_to_twice(n): return (2*n)+65536def add_65537_to_twice(n): return (2*n)+65537...

Page 21: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

21

PROBLEM: MAKING ADDERSdef add_1_to_twice(n): return (2*n)+1def add_2_to_twice(n): return (2*n)+2def add_3_to_twice(n): return (2*n)+3...def add_65536_to_twice(n): return (2*n)+65536def add_65537_to_twice(n): return (2*n)+65537...

There has to be a better way!

Page 22: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

22

PROBLEM: MAKING ADDERSdef add_1_to_twice(n): return (2*n)+1def add_2_to_twice(n): return (2*n)+2def add_3_to_twice(n): return (2*n)+3...def add_65536_to_twice(n): return (2*n)+65536def add_65537_to_twice(n): return (2*n)+65537...

Can we generalize?

Page 23: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

23

PROBLEM: MAKING ADDERS

http://farm8.staticflickr.com/7014/6766279501_4ddeb9193c_z.jpg

Page 24: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

24

PROBLEM: MAKING ADDERS def make_adder_for(m): def add_m_to_twice(n): return (2*n)+m return add_m_to_twice

add_1_to_twice = make_adder_for(1)add_2_to_twice = make_adder_for(2)add_3_to_twice = make_adder_for(3)...add_65536_to_twice = make_adder_for(65536)add_65537_to_twice = make_adder_for(65537)...

Page 25: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

25

make_adder_for(m):

return add_m_to_twice

PROBLEM: MAKING ADDERSdef make_adder_for(m): def add_m_to_twice(n): return (2*n)+m return add_m_to_twiceadd_m_to_twice(n):

return (2 * n) + m

3

add_m_to_twice(n): return (2 * n) + 3

Page 26: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

26

I want to make specialized sums for later use. Complete the definition below:

def make_summationer(term):def summation(n):

k, sum = 1, 0while k <= n:

sum = sum + term(k)k = k + 1

return sumreturn summation

PRACTICE: MAKING SUMMATION...ERS

Page 27: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

27

I want to make specialized sums for later use. Complete the definition below:

def make_summationer(term):def summation(n):

k, sum = 1, 0while k <= n:

sum = sum + term(k)k = k + 1

return sumreturn summation

PRACTICE: MAKING SUMMATION...ERS

Page 28: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

28

PRACTICE: MAKING ADDERS FOR SUMMATIONS

Say I wanted to get the sum of the numbers 4 through 17. How can I do this in one line using summation and make_adder?def make_adder(n): def add_n(m): return n + m return add_ndef summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum>>> summation(14, make_adder(3))147

??!?!?!??!?!

Page 29: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

29

PRACTICE: MAKING ADDERS FOR SUMMATIONS

Say I wanted to get the sum of the numbers 4 through 17. How can I do this in one line using summation and make_adder?def make_adder(n): def add_n(m): return n + m return add_ndef summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum>>> summation(14, make_adder(3))147

“k + 3”

“k + 3”

Page 30: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

30

ANONYMOUS FUNCTIONS

What if we don’t want to give a name to our function? Seems silly that we have to come up with a name that we’re only using once in our program (in the return statement).

def make_adder(n): def add_n(m): return n + m return add_n

Can I do the same thing without providing a name?– Yes!

Page 31: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

31

ANONYMOUS FUNCTIONS

In Computer Science, we traditionally call anonymous function values “lambda functions.”

In Python a lambda function has the form:

lambda <arguments>: <expression>

For example we could have done:

def make_adder(n): return lambda m: n + m

λ

Page 32: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

32

ANONYMOUS FUNCTIONS

Translating between lambdas and defined functions.

λ

<name> = lambda <arguments>: <expression>

def <name>(<arguments>):return <expression>

Page 33: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

33

ANONYMOUS FUNCTIONS

Translating between lambdas and defined functions.

λ

square = lambda x: x * x

def square(x):return x*x

Page 34: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

34

ANONYMOUS FUNCTIONS

Translating between lambdas and defined functions.

λ

foo = lambda a, x, b: (a * x) + b

def foo(a, x, b):return (a * x) + b

Page 35: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

35

ANONYMOUS FUNCTIONS

Translating between lambdas and defined functions.

λ

def make_adder(n): return lambda m: m + n

def make_adder(n): def add_n(m): return m + n return add_n

Page 36: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

36

ANONYMOUS FUNCTIONS

Key points about lambda in Python:– A lambda in Python is an expression, so you can use

them anywhere you can use any other expression in Python.

– You can only use a single expression in the body of a lambda function.

– A lambda function always returns the value of the body expression.

– Lambdas should be used sparingly, only for very simple functions. Otherwise the code can quickly become unreadable.

λ

Page 37: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

37

PRACTICE: ANONYMOUS FUNCTIONS

What would the following expression evaluate to?

>>> summation(3, lambda x: x + 5)21

Page 38: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

38

PRACTICE: ANONYMOUS FUNCTIONS

What would the following expression evaluate to?

>>> summation(3, lambda x: x + 5)21

Page 39: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

39

CONCLUSION

• Functions can be defined inside other functions!

• Functions can return functions.• Python has lambda functions for creating

anonymous functions.• Next Time: More practice with HOFs, and

practical applications!

Page 40: CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.

40

EXTRAS: MAKING MY ADDERAND USING IT TOO!

We can add 3 and 5, using only the values make_adder, 3, and 5.

make_adder(3) ( 5 )

5make_adder ( 3 )

3

8

make_adder

add_n*

* Where n is equal to 3