CS61A Lecture 5 Amir Kamil UC Berkeley February 1, 2013 - - PowerPoint PPT Presentation

cs61a lecture 5
SMART_READER_LITE
LIVE PREVIEW

CS61A Lecture 5 Amir Kamil UC Berkeley February 1, 2013 - - PowerPoint PPT Presentation

CS61A Lecture 5 Amir Kamil UC Berkeley February 1, 2013 Announcements Quiz today! Only worth two points, so dont worry! Hog project Get started early! If you still dont have a partner (and want one), find one on Piazza


slide-1
SLIDE 1

CS61A Lecture 5

Amir Kamil UC Berkeley February 1, 2013

slide-2
SLIDE 2

 Quiz today!

 Only worth two points, so don’t worry!

 Hog project

 Get started early!  If you still don’t have a partner (and want one), find one

  • n Piazza

 Use existing post; don’t make a new one

Announcements

slide-3
SLIDE 3

   

The Art of the Function

slide-4
SLIDE 4

 Give each function exactly one job   

The Art of the Function

slide-5
SLIDE 5

 Give each function exactly one job  Don’t reapeat yourself (DRY).  

The Art of the Function

slide-6
SLIDE 6

 Give each function exactly one job  Don’t reapeat yourself (DRY).  Don’t reapeat yourself (DRY). 

The Art of the Function

slide-7
SLIDE 7

 Give each function exactly one job  Don’t reapeat yourself (DRY).  Don’t reapeat yourself (DRY).  Define functions generally

The Art of the Function

slide-8
SLIDE 8

Generalizing Patterns with Parameters

slide-9
SLIDE 9

Generalizing Patterns with Parameters

Regular geometric shapes relate length and area.

slide-10
SLIDE 10

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area.

slide-11
SLIDE 11

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area.

slide-12
SLIDE 12

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area.

slide-13
SLIDE 13

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area.

slide-14
SLIDE 14

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-15
SLIDE 15

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-16
SLIDE 16

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-17
SLIDE 17

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-18
SLIDE 18

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-19
SLIDE 19

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-20
SLIDE 20

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-21
SLIDE 21

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area:

slide-22
SLIDE 22

Generalizing Patterns with Parameters

Shape: Regular geometric shapes relate length and area. Area: Finding common structure allows for shared implementation

slide-23
SLIDE 23

Generalizing Over Computational Processes

slide-24
SLIDE 24

Generalizing Over Computational Processes

The common structure among functions may itself be a computational process, rather than a number.

slide-25
SLIDE 25

Generalizing Over Computational Processes

The common structure among functions may itself be a computational process, rather than a number.

slide-26
SLIDE 26

Generalizing Over Computational Processes

The common structure among functions may itself be a computational process, rather than a number.

slide-27
SLIDE 27

Generalizing Over Computational Processes

The common structure among functions may itself be a computational process, rather than a number.

slide-28
SLIDE 28

Generalizing Over Computational Processes

The common structure among functions may itself be a computational process, rather than a number.

slide-29
SLIDE 29

Functions as Arguments

slide-30
SLIDE 30

Function values can be passed as arguments

Functions as Arguments

slide-31
SLIDE 31

def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

Function values can be passed as arguments

Functions as Arguments

slide-32
SLIDE 32

def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

Function values can be passed as arguments

Functions as Arguments

Function of a single argument (not called term)

slide-33
SLIDE 33

def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

Function values can be passed as arguments

Functions as Arguments

Function of a single argument (not called term) A formal parameter that will be bound to a function

slide-34
SLIDE 34

def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

Function values can be passed as arguments

Functions as Arguments

Function of a single argument (not called term) A formal parameter that will be bound to a function The function bound to term gets called here

slide-35
SLIDE 35

def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

Function values can be passed as arguments

Functions as Arguments

Function of a single argument (not called term) A formal parameter that will be bound to a function The function bound to term gets called here The cube function is passed as an argument value

slide-36
SLIDE 36

def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

Function values can be passed as arguments

Functions as Arguments

Function of a single argument (not called term) A formal parameter that will be bound to a function The function bound to term gets called here The cube function is passed as an argument value 0 + 13 + 23 + 33 + 43 + 55

slide-37
SLIDE 37

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-38
SLIDE 38

Parameters can be bound to function values

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-39
SLIDE 39

Parameters can be bound to function values

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-40
SLIDE 40

Parameters can be bound to function values

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-41
SLIDE 41

Parameters can be bound to function values

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-42
SLIDE 42

Parameters can be bound to function values

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-43
SLIDE 43

Parameters can be bound to function values

Function Values as Parameters

Example: http://goo.gl/e4YBH

slide-44
SLIDE 44

 

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

slide-45
SLIDE 45

Locally defined functions can be returned

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

slide-46
SLIDE 46

Locally defined functions can be returned They have access to the frame in which they are defined

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

slide-47
SLIDE 47

Locally defined functions can be returned They have access to the frame in which they are defined

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder A function that returns a function

slide-48
SLIDE 48

Locally defined functions can be returned They have access to the frame in which they are defined

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder A function that returns a function A local def statement

slide-49
SLIDE 49

Locally defined functions can be returned They have access to the frame in which they are defined

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder A function that returns a function A local def statement The name add_three is bound to a function

slide-50
SLIDE 50

Locally defined functions can be returned They have access to the frame in which they are defined

Functions as Return Values

def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder A function that returns a function A local def statement Can refer to names in the enclosing function The name add_three is bound to a function

slide-51
SLIDE 51

Call Expressions as Operators

def make_adder(n): def adder(k): return add(n, k) return adder make_adder(1)(2)

slide-52
SLIDE 52

Call Expressions as Operators

def make_adder(n): def adder(k): return add(n, k) return adder make_adder(1)(2) make_adder(1) ( 2 )

slide-53
SLIDE 53

Call Expressions as Operators

def make_adder(n): def adder(k): return add(n, k) return adder make_adder(1)(2) make_adder(1) ( 2 ) Operator

slide-54
SLIDE 54

Call Expressions as Operators

def make_adder(n): def adder(k): return add(n, k) return adder make_adder(1)(2) make_adder(1) ( 2 ) Operator Operand 0

slide-55
SLIDE 55

Call Expressions as Operators

def make_adder(n): def adder(k): return add(n, k) return adder make_adder(1)(2) make_adder(1) ( 2 ) Operator Operand 0 An expression that evaluates to a function value

slide-56
SLIDE 56

Call Expressions as Operators

def make_adder(n): def adder(k): return add(n, k) return adder make_adder(1)(2) make_adder(1) ( 2 ) Operator Operand 0 An expression that evaluates to a function value An expression that evaluates to any value

slide-57
SLIDE 57

  

  

Higher‐Order Functions

slide-58
SLIDE 58

Functions are first‐class: they can be manipulated as values in Python

 

  

Higher‐Order Functions

slide-59
SLIDE 59

Functions are first‐class: they can be manipulated as values in Python Higher‐order function: a function that takes a function as an argument value or returns a function as a return value

  

Higher‐Order Functions

slide-60
SLIDE 60

Functions are first‐class: they can be manipulated as values in Python Higher‐order function: a function that takes a function as an argument value or returns a function as a return value Higher order functions:

  

Higher‐Order Functions

slide-61
SLIDE 61

Functions are first‐class: they can be manipulated as values in Python Higher‐order function: a function that takes a function as an argument value or returns a function as a return value Higher order functions:

 Express general methods of computation  

Higher‐Order Functions

slide-62
SLIDE 62

Functions are first‐class: they can be manipulated as values in Python Higher‐order function: a function that takes a function as an argument value or returns a function as a return value Higher order functions:

 Express general methods of computation  Remove repetition from programs 

Higher‐Order Functions

slide-63
SLIDE 63

Functions are first‐class: they can be manipulated as values in Python Higher‐order function: a function that takes a function as an argument value or returns a function as a return value Higher order functions:

 Express general methods of computation  Remove repetition from programs  Separate concerns among functions

Higher‐Order Functions