61A Lecture 4 Monday, September 9 Announcements Homework 1 due - - PowerPoint PPT Presentation

61a lecture 4
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 4 Monday, September 9 Announcements Homework 1 due - - PowerPoint PPT Presentation

61A Lecture 4 Monday, September 9 Announcements Homework 1 due Tuesday 9/10 at 5pm; Late homework is not accepted! Quiz on Wednesday 9/11 released at 1pm, due Thursday 9/12 at 11:59pm Open-computer : You can use the Python


slide-1
SLIDE 1

61A Lecture 4

Monday, September 9

slide-2
SLIDE 2

Announcements

  • Homework 1 due Tuesday 9/10 at 5pm; Late homework is not accepted!
  • Quiz on Wednesday 9/11 released at 1pm, due Thursday 9/12 at 11:59pm
  • Open-computer: You can use the Python interpreter, watch course videos, and read the
  • nline text (http://composingprograms.com).
  • No external resources: Please don't search for answers, talk to your classmates, etc.
  • Content Covered: Lectures through last Friday 9/6; Same topics as Homework 1.
  • Project 1 due next Thursday 9/19 at 11:59pm

2

slide-3
SLIDE 3

Iteration Example

slide-4
SLIDE 4

The Fibonacci Sequence

Example: http://goo.gl/vfymhd

4

slide-5
SLIDE 5

The Fibonacci Sequence

Example: http://goo.gl/vfymhd

4

slide-6
SLIDE 6

The Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

slide-7
SLIDE 7

The Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

slide-8
SLIDE 8

The Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-9
SLIDE 9

The Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-10
SLIDE 10

The Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-11
SLIDE 11

The Fibonacci Sequence

def fib(n): 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-12
SLIDE 12

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-13
SLIDE 13

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-14
SLIDE 14

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-15
SLIDE 15

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-16
SLIDE 16

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

2

1 1

slide-17
SLIDE 17

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-18
SLIDE 18

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-19
SLIDE 19

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-20
SLIDE 20

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-21
SLIDE 21

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-22
SLIDE 22

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-23
SLIDE 23

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-24
SLIDE 24

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-25
SLIDE 25

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-26
SLIDE 26

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fibonacci number is called current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Example: http://goo.gl/vfymhd

4

5

8

13

21

34

3

The next Fibonacci number is the sum of the current one and its predecessor

2

1 1

slide-27
SLIDE 27

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

Example: http://goo.gl/38ch3o

slide-28
SLIDE 28

def choose(total, selection):

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

Example: http://goo.gl/38ch3o

slide-29
SLIDE 29

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL.

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

Example: http://goo.gl/38ch3o

slide-30
SLIDE 30

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k!

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

Example: http://goo.gl/38ch3o

slide-31
SLIDE 31

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k!

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-32
SLIDE 32

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-33
SLIDE 33

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-34
SLIDE 34

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-35
SLIDE 35

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-36
SLIDE 36

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-37
SLIDE 37

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-38
SLIDE 38

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________, total - 1 return ways n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

Example: http://goo.gl/38ch3o

slide-39
SLIDE 39

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________, total - 1 return ways n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

total // selected

Example: http://goo.gl/38ch3o

slide-40
SLIDE 40

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________, total - 1 return ways n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

total // selected

Example: http://goo.gl/38ch3o

slide-41
SLIDE 41

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________, total - 1 return ways n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

total // selected

Example: http://goo.gl/38ch3o

slide-42
SLIDE 42

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________, total - 1 return ways n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

total // selected

Example: http://goo.gl/38ch3o

slide-43
SLIDE 43

def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """

Discussion Question

Complete the following definition by placing an expression in ______________________.

5

ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________, total - 1 return ways n · (n − 1) · (n − 2) · . . . · (n − k + 1) k · (k − 1) · (k − 2) · . . . · 2 · 1

total // selected ... ...

Example: http://goo.gl/38ch3o

slide-44
SLIDE 44

Default Arguments

(Demo)

slide-45
SLIDE 45

Designing Functions

slide-46
SLIDE 46

Characteristics of Functions

8

slide-47
SLIDE 47

A function's domain is the set of all inputs it might possibly take as arguments.

Characteristics of Functions

8

slide-48
SLIDE 48

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return.

Characteristics of Functions

8

slide-49
SLIDE 49

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

slide-50
SLIDE 50

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X."""

slide-51
SLIDE 51

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items."""

slide-52
SLIDE 52

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" x is a number

slide-53
SLIDE 53

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" x is a number n and d are positive integers with n greater than or equal to d.

slide-54
SLIDE 54

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" x is a number n and d are positive integers with n greater than or equal to d. return value is a positive number

slide-55
SLIDE 55

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" x is a number n and d are positive integers with n greater than or equal to d. return value is a positive number return value is a positive integer

slide-56
SLIDE 56

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" x is a number n and d are positive integers with n greater than or equal to d. return value is a positive number return value is a positive integer return value is the square of the input

slide-57
SLIDE 57

A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output.

Characteristics of Functions

8

def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" x is a number n and d are positive integers with n greater than or equal to d. return value is a positive number return value is a positive integer return value is the square of the input return value is the number of ways to choose d of n items.

slide-58
SLIDE 58

A Guide to Designing Function

9

slide-59
SLIDE 59

Give each function exactly one job.

A Guide to Designing Function

9

slide-60
SLIDE 60

Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times.

A Guide to Designing Function

9

slide-61
SLIDE 61

Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally.

A Guide to Designing Function

9

slide-62
SLIDE 62

Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally.

A Guide to Designing Function

9

slide-63
SLIDE 63

Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. not

A Guide to Designing Function

9

slide-64
SLIDE 64

Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. not

A Guide to Designing Function

9

slide-65
SLIDE 65

Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. not

A Guide to Designing Function

9

slide-66
SLIDE 66

Generalization

slide-67
SLIDE 67

Generalizing Patterns with Arguments

11

slide-68
SLIDE 68

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

11

slide-69
SLIDE 69

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

11

slide-70
SLIDE 70

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r

11

slide-71
SLIDE 71

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r

11

slide-72
SLIDE 72

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

11

slide-73
SLIDE 73

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-74
SLIDE 74

Shape:

r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-75
SLIDE 75

Shape:

r2 π · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-76
SLIDE 76

Shape:

r2 π · r2 3 √ 3 2 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-77
SLIDE 77

Shape:

r2 π · r2 3 √ 3 2 · r2 1 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-78
SLIDE 78

Shape:

r2 π · r2 3 √ 3 2 · r2 1 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-79
SLIDE 79

Shape:

r2 π · r2 3 √ 3 2 · r2 1 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-80
SLIDE 80

Shape:

r2 π · r2 3 √ 3 2 · r2 1 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area:

11

slide-81
SLIDE 81

Shape:

r2 π · r2 3 √ 3 2 · r2 1 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area: Finding common structure allows for shared implementation

11

slide-82
SLIDE 82

Shape:

r2 π · r2 3 √ 3 2 · r2 1 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

r r r

Area: Finding common structure allows for shared implementation

11

(Demo)

slide-83
SLIDE 83

Higher-Order Functions

slide-84
SLIDE 84

Generalizing Over Computational Processes

13

slide-85
SLIDE 85

Generalizing Over Computational Processes

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

13

slide-86
SLIDE 86

5

X

k=1

k = 1 + 2 + 3 + 4 + 5 = 15

5

X

k=1

k3 = 13 + 23 + 33 + 43 + 53 = 225

5

X

k=1

8 (4k − 3) · (4k − 1) = 8 3 + 8 35 + 8 99 + 8 195 + 8 323 = 3.04

Generalizing Over Computational Processes

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

13

slide-87
SLIDE 87

5

X

k=1

k = 1 + 2 + 3 + 4 + 5 = 15

5

X

k=1

k3 = 13 + 23 + 33 + 43 + 53 = 225

5

X

k=1

8 (4k − 3) · (4k − 1) = 8 3 + 8 35 + 8 99 + 8 195 + 8 323 = 3.04

Generalizing Over Computational Processes

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

13

slide-88
SLIDE 88

5

X

k=1

k = 1 + 2 + 3 + 4 + 5 = 15

5

X

k=1

k3 = 13 + 23 + 33 + 43 + 53 = 225

5

X

k=1

8 (4k − 3) · (4k − 1) = 8 3 + 8 35 + 8 99 + 8 195 + 8 323 = 3.04

Generalizing Over Computational Processes

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

13

slide-89
SLIDE 89

5

X

k=1

k = 1 + 2 + 3 + 4 + 5 = 15

5

X

k=1

k3 = 13 + 23 + 33 + 43 + 53 = 225

5

X

k=1

8 (4k − 3) · (4k − 1) = 8 3 + 8 35 + 8 99 + 8 195 + 8 323 = 3.04

Generalizing Over Computational Processes

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

13

slide-90
SLIDE 90

5

X

k=1

k = 1 + 2 + 3 + 4 + 5 = 15

5

X

k=1

k3 = 13 + 23 + 33 + 43 + 53 = 225

5

X

k=1

8 (4k − 3) · (4k − 1) = 8 3 + 8 35 + 8 99 + 8 195 + 8 323 = 3.04

Generalizing Over Computational Processes

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

13

(Demo)

slide-91
SLIDE 91

Summation Example 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 − − −−

14

slide-92
SLIDE 92

Summation Example 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 of a single argument (not called term)

14

slide-93
SLIDE 93

Summation Example 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 of a single argument (not called term) A formal parameter that will be bound to a function

14

slide-94
SLIDE 94

Summation Example 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 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

14

slide-95
SLIDE 95

Summation Example 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 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

14

slide-96
SLIDE 96

Summation Example 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 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 + 53

14

slide-97
SLIDE 97

Functions as Return Values

(Demo)

slide-98
SLIDE 98

Locally Defined Functions

16

slide-99
SLIDE 99

Locally Defined Functions

Functions defined within other function bodies are bound to names in a local frame

16

slide-100
SLIDE 100

Locally Defined Functions

− − def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder −− Functions defined within other function bodies are bound to names in a local frame

16

slide-101
SLIDE 101

Locally Defined Functions

− − def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder −− A function that returns a function Functions defined within other function bodies are bound to names in a local frame

16

slide-102
SLIDE 102

Locally Defined Functions

− − def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder −− A function that returns a function The name add_three is bound to a function Functions defined within other function bodies are bound to names in a local frame

16

slide-103
SLIDE 103

Locally Defined Functions

− − def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder −− A function that returns a function A local def statement The name add_three is bound to a function Functions defined within other function bodies are bound to names in a local frame

16

slide-104
SLIDE 104

Locally Defined Functions

− − def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder −− A function that returns a function A local def statement The name add_three is bound to a function Can refer to names in the enclosing function Functions defined within other function bodies are bound to names in a local frame

16

slide-105
SLIDE 105

Call Expressions as Operator Expressions

17

def make_adder(n): def adder(k): return k + n return adder

slide-106
SLIDE 106

Call Expressions as Operator Expressions

make_adder(1) ( 2 )

17

def make_adder(n): def adder(k): return k + n return adder

slide-107
SLIDE 107

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator

17

def make_adder(n): def adder(k): return k + n return adder

slide-108
SLIDE 108

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand

17

def make_adder(n): def adder(k): return k + n return adder

slide-109
SLIDE 109

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function

17

def make_adder(n): def adder(k): return k + n return adder

slide-110
SLIDE 110

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

slide-111
SLIDE 111

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

slide-112
SLIDE 112

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

make_adder(1)

slide-113
SLIDE 113

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

make_adder(1) func make_adder(n)

slide-114
SLIDE 114

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

make_adder(1) func make_adder(n) 1

slide-115
SLIDE 115

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

make_adder(1) func adder(k) func make_adder(n) 1

slide-116
SLIDE 116

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

2 make_adder(1) func adder(k) func make_adder(n) 1

slide-117
SLIDE 117

Call Expressions as Operator Expressions

make_adder(1) ( 2 ) Operator Operand An expression that evaluates to a function An expression that evaluates to any value

17

def make_adder(n): def adder(k): return k + n return adder

2 3 make_adder(1) func adder(k) func make_adder(n) 1

slide-118
SLIDE 118

The Purpose of Higher-Order Functions

18

slide-119
SLIDE 119

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in

  • ur programming language.

18

slide-120
SLIDE 120

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in

  • ur programming language.

Higher-order function: A function that takes a function as an argument value or returns a function as a return value

18

slide-121
SLIDE 121

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in

  • ur programming language.

Higher-order functions: Higher-order function: A function that takes a function as an argument value or returns a function as a return value

18

slide-122
SLIDE 122

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in

  • ur programming language.

Higher-order functions:

  • Express general methods of computation

Higher-order function: A function that takes a function as an argument value or returns a function as a return value

18

slide-123
SLIDE 123

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in

  • ur programming language.

Higher-order functions:

  • Express general methods of computation
  • Remove repetition from programs

Higher-order function: A function that takes a function as an argument value or returns a function as a return value

18

slide-124
SLIDE 124

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in

  • ur programming language.

Higher-order functions:

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

Higher-order function: A function that takes a function as an argument value or returns a function as a return value

18

slide-125
SLIDE 125

The Game of Hog

(Demo)