61A Lecture 4 Friday, August 31 The Fibonacci Sequence 2 Example: - - PowerPoint PPT Presentation

61a lecture 4
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 4 Friday, August 31 The Fibonacci Sequence 2 Example: - - PowerPoint PPT Presentation

61A Lecture 4 Friday, August 31 The Fibonacci Sequence 2 Example: http://goo.gl/dcaf0 The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... 2 Example: http://goo.gl/dcaf0 The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n):


slide-1
SLIDE 1

61A Lecture 4

Friday, August 31

slide-2
SLIDE 2

The Fibonacci Sequence

2

Example: http://goo.gl/dcaf0

slide-3
SLIDE 3

The Fibonacci Sequence

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-4
SLIDE 4

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-5
SLIDE 5

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-6
SLIDE 6

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-7
SLIDE 7

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-8
SLIDE 8

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-9
SLIDE 9

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-10
SLIDE 10

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-11
SLIDE 11

The Fibonacci Sequence

def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr

2

0, 1, 1, 2, 3, 5, 8, 13, ...

Example: http://goo.gl/dcaf0

slide-12
SLIDE 12

Practical Guidance: the Art of the Function

3

slide-13
SLIDE 13

Practical Guidance: the Art of the Function

3

slide-14
SLIDE 14

Give each function exactly one job.

Practical Guidance: the Art of the Function

3

slide-15
SLIDE 15

vs Give each function exactly one job.

Practical Guidance: the Art of the Function

3

slide-16
SLIDE 16

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

Practical Guidance: the Art of the Function

3

slide-17
SLIDE 17

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

Practical Guidance: the Art of the Function

3

slide-18
SLIDE 18

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

Practical Guidance: the Art of the Function

3

slide-19
SLIDE 19

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

Practical Guidance: the Art of the Function

3

slide-20
SLIDE 20

Generalizing Patterns with Arguments

4

slide-21
SLIDE 21

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

slide-22
SLIDE 22

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

slide-23
SLIDE 23

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r

slide-24
SLIDE 24

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r

slide-25
SLIDE 25

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

slide-26
SLIDE 26

Shape:

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-27
SLIDE 27

Shape:

r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-28
SLIDE 28

Shape:

r2 π · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-29
SLIDE 29

Shape:

r2 π · r2 3 √ 3 2 · r2

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-30
SLIDE 30

Shape:

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

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-31
SLIDE 31

Shape:

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

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-32
SLIDE 32

Shape:

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

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-33
SLIDE 33

Shape:

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

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area:

slide-34
SLIDE 34

Shape:

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

Generalizing Patterns with Arguments

Regular geometric shapes relate length and area.

4

r r r

Area: Finding common structure allows for shared implementation

slide-35
SLIDE 35

Generalizing Over Computational Processes

5

slide-36
SLIDE 36

Generalizing Over Computational Processes

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

5

slide-37
SLIDE 37

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 itself be a computational process, rather than a number.

5

slide-38
SLIDE 38

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 itself be a computational process, rather than a number.

5

slide-39
SLIDE 39

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 itself be a computational process, rather than a number.

5

slide-40
SLIDE 40

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 itself be a computational process, rather than a number.

5

slide-41
SLIDE 41

Summation Example

6

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

slide-42
SLIDE 42

Summation Example

6

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)

slide-43
SLIDE 43

Summation Example

6

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

slide-44
SLIDE 44

Summation Example

6

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

slide-45
SLIDE 45

Summation Example

6

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

slide-46
SLIDE 46

Summation Example

6

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 + 55

slide-47
SLIDE 47

Locally Defined Functions

7

slide-48
SLIDE 48

Locally Defined Functions

7

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

slide-49
SLIDE 49

Locally Defined Functions

7

− − 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 the local frame

slide-50
SLIDE 50

Locally Defined Functions

7

− − 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 the local frame

slide-51
SLIDE 51

Locally Defined Functions

7

− − 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 the local frame

slide-52
SLIDE 52

Locally Defined Functions

7

− − 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 the local frame

slide-53
SLIDE 53

Locally Defined Functions

7

− − 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 the local frame

slide-54
SLIDE 54

Call Expressions as Operator Expressions

8

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

slide-55
SLIDE 55

Call Expressions as Operator Expressions

8

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

slide-56
SLIDE 56

Call Expressions as Operator Expressions

8

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

slide-57
SLIDE 57

Call Expressions as Operator Expressions

8

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

slide-58
SLIDE 58

Call Expressions as Operator Expressions

8

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

slide-59
SLIDE 59

Call Expressions as Operator Expressions

8

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

slide-60
SLIDE 60

The Purpose of Higher-Order Functions

9

slide-61
SLIDE 61

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in our programming language.

9

slide-62
SLIDE 62

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in our programming language.

9

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

slide-63
SLIDE 63

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in our programming language.

9

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

slide-64
SLIDE 64

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in our programming language.

9

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

slide-65
SLIDE 65

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in our programming language.

9

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

slide-66
SLIDE 66

The Purpose of Higher-Order Functions

Functions are first-class: Functions can be manipulated as values in our programming language.

9

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

slide-67
SLIDE 67

Pig Introduction

(Demo)

10