Environments Announcements Environments for Higher-Order Functions - - PowerPoint PPT Presentation

environments announcements environments for higher order
SMART_READER_LITE
LIVE PREVIEW

Environments Announcements Environments for Higher-Order Functions - - PowerPoint PPT Presentation

Environments Announcements Environments for Higher-Order Functions Environments Enable Higher-Order Functions 4 Environments Enable Higher-Order Functions Functions are first-class: Functions are values in our programming language 4


slide-1
SLIDE 1

Environments

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Environments for Higher-Order Functions

slide-4
SLIDE 4

Environments Enable Higher-Order Functions

4

slide-5
SLIDE 5

Environments Enable Higher-Order Functions

4

Functions are first-class: Functions are values in our programming language

slide-6
SLIDE 6

Environments Enable Higher-Order Functions

4

Functions are first-class: Functions are values in our programming language Higher-order function: A function that takes a function as an argument value or A function that returns a function as a return value

slide-7
SLIDE 7

Environments Enable Higher-Order Functions

4

Environment diagrams describe how higher-order functions work! Functions are first-class: Functions are values in our programming language Higher-order function: A function that takes a function as an argument value or A function that returns a function as a return value

slide-8
SLIDE 8

Environments Enable Higher-Order Functions

(Demo)

4

Environment diagrams describe how higher-order functions work! Functions are first-class: Functions are values in our programming language Higher-order function: A function that takes a function as an argument value or A function that returns a function as a return value

slide-9
SLIDE 9

Names can be Bound to Functional Arguments

5

pythontutor.com/composingprograms.html#code=def%20apply_twice%28f,%20x%29%3A%0A%20%20%20%20return%20f%28f%28x%29%29%0A%20%20%20%20%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%20%20%20%20%0Aresult%20%3D%20apply_twice%28square,%202%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

slide-10
SLIDE 10

Names can be Bound to Functional Arguments

5

pythontutor.com/composingprograms.html#code=def%20apply_twice%28f,%20x%29%3A%0A%20%20%20%20return%20f%28f%28x%29%29%0A%20%20%20%20%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%20%20%20%20%0Aresult%20%3D%20apply_twice%28square,%202%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

slide-11
SLIDE 11

Names can be Bound to Functional Arguments

5

Applying a user-defined function:

  • Create a new frame
  • Bind formal parameters

(f & x) to arguments

  • Execute the body:

return f(f(x))

pythontutor.com/composingprograms.html#code=def%20apply_twice%28f,%20x%29%3A%0A%20%20%20%20return%20f%28f%28x%29%29%0A%20%20%20%20%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%20%20%20%20%0Aresult%20%3D%20apply_twice%28square,%202%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

slide-12
SLIDE 12

Names can be Bound to Functional Arguments

5

Applying a user-defined function:

  • Create a new frame
  • Bind formal parameters

(f & x) to arguments

  • Execute the body:

return f(f(x))

pythontutor.com/composingprograms.html#code=def%20apply_twice%28f,%20x%29%3A%0A%20%20%20%20return%20f%28f%28x%29%29%0A%20%20%20%20%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%20%20%20%20%0Aresult%20%3D%20apply_twice%28square,%202%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

slide-13
SLIDE 13

Names can be Bound to Functional Arguments

5

Applying a user-defined function:

  • Create a new frame
  • Bind formal parameters

(f & x) to arguments

  • Execute the body:

return f(f(x))

pythontutor.com/composingprograms.html#code=def%20apply_twice%28f,%20x%29%3A%0A%20%20%20%20return%20f%28f%28x%29%29%0A%20%20%20%20%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%20%20%20%20%0Aresult%20%3D%20apply_twice%28square,%202%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

2 1

slide-14
SLIDE 14

Names can be Bound to Functional Arguments

5

Applying a user-defined function:

  • Create a new frame
  • Bind formal parameters

(f & x) to arguments

  • Execute the body:

return f(f(x))

pythontutor.com/composingprograms.html#code=def%20apply_twice%28f,%20x%29%3A%0A%20%20%20%20return%20f%28f%28x%29%29%0A%20%20%20%20%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%20%20%20%20%0Aresult%20%3D%20apply_twice%28square,%202%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

2 1

slide-15
SLIDE 15

Environments for Nested Definitions

(Demo)

slide-16
SLIDE 16

Environment Diagrams for Nested Def Statements

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-17
SLIDE 17

Environment Diagrams for Nested Def Statements

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-18
SLIDE 18

Environment Diagrams for Nested Def Statements

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-19
SLIDE 19

Environment Diagrams for Nested Def Statements

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-20
SLIDE 20

Environment Diagrams for Nested Def Statements

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-21
SLIDE 21

Environment Diagrams for Nested Def Statements

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-22
SLIDE 22

Environment Diagrams for Nested Def Statements

2 1 3

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-23
SLIDE 23

Environment Diagrams for Nested Def Statements

2 1 3

Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-24
SLIDE 24

Environment Diagrams for Nested Def Statements

2 1 3

  • Every user-defined function has

a parent frame (often global) Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-25
SLIDE 25

Environment Diagrams for Nested Def Statements

2 1 3

  • Every user-defined function has

a parent frame (often global)

  • The parent of a function is the

frame in which it was defined Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-26
SLIDE 26

Environment Diagrams for Nested Def Statements

2 1 3

  • Every user-defined function has

a parent frame (often global)

  • The parent of a function is the

frame in which it was defined

  • Every local frame has a parent

frame (often global) Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-27
SLIDE 27

Environment Diagrams for Nested Def Statements

2 1 3

  • Every user-defined function has

a parent frame (often global)

  • The parent of a function is the

frame in which it was defined

  • Every local frame has a parent

frame (often global)

  • The parent of a frame is the

parent of the function called Nested def

7

http://pythontutor.com/composingprograms.html#code=def%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Athree_more_than%20%3D%20make_adder%283%29%0Aresult%20%3D%20three_more_than%284%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-28
SLIDE 28

How to Draw an Environment Diagram

8

slide-29
SLIDE 29

How to Draw an Environment Diagram

When a function is defined:

8

slide-30
SLIDE 30

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>]

8

slide-31
SLIDE 31

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame.

8

slide-32
SLIDE 32

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame.

8

slide-33
SLIDE 33

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame. Bind <name> to the function value in the current frame

8

slide-34
SLIDE 34

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame. Bind <name> to the function value in the current frame When a function is called:

8

slide-35
SLIDE 35

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame. Bind <name> to the function value in the current frame When a function is called:

  • 1. Add a local frame, titled with the <name> of the function being called.

8

slide-36
SLIDE 36

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame. Bind <name> to the function value in the current frame When a function is called:

  • 1. Add a local frame, titled with the <name> of the function being called.
  • 2. Copy the parent of the function to the local frame: [parent=<label>]

8

slide-37
SLIDE 37

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame. Bind <name> to the function value in the current frame When a function is called:

  • 1. Add a local frame, titled with the <name> of the function being called.
  • 2. Copy the parent of the function to the local frame: [parent=<label>]
  • 3. Bind the <formal parameters> to the arguments in the local frame.

8

slide-38
SLIDE 38

How to Draw an Environment Diagram

When a function is defined: Create a function value: func <name>(<formal parameters>) [parent=<label>] Its parent is the current frame. Bind <name> to the function value in the current frame When a function is called:

  • 1. Add a local frame, titled with the <name> of the function being called.
  • 2. Copy the parent of the function to the local frame: [parent=<label>]
  • 3. Bind the <formal parameters> to the arguments in the local frame.
  • 4. Execute the body of the function in the environment that starts with the local frame.

8

slide-39
SLIDE 39

Local Names

(Demo)

slide-40
SLIDE 40

Local Names are not Visible to Other (Non-Nested) Functions

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-41
SLIDE 41

Local Names are not Visible to Other (Non-Nested) Functions

2 1

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-42
SLIDE 42

Local Names are not Visible to Other (Non-Nested) Functions

2 1

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-43
SLIDE 43

Local Names are not Visible to Other (Non-Nested) Functions

2 1

“y” is not found

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-44
SLIDE 44

Local Names are not Visible to Other (Non-Nested) Functions

2 1

“y” is not found “y” is not found, again

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-45
SLIDE 45

Local Names are not Visible to Other (Non-Nested) Functions

2 1

“y” is not found “y” is not found, again Error

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-46
SLIDE 46

Local Names are not Visible to Other (Non-Nested) Functions

2 1

“y” is not found “y” is not found, again Error

  • An environment is a sequence of frames.

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-47
SLIDE 47

Local Names are not Visible to Other (Non-Nested) Functions

2 1

“y” is not found “y” is not found, again Error

  • An environment is a sequence of frames.
  • The environment created by calling a top-level function (no def within def)

consists of one local frame, followed by the global frame.

10

http://pythontutor.com/composingprograms.html#code=def%20f%28x, %20y%29%3A%0A%20%20%20%20return%20g%28x%29%0A%0Adef%20g%28a%29%3A%0A%20%20%20%20return%20a%20%2B%20y%0A%20%20%20%20%0Aresult%20%3D%20f%281,%202%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-48
SLIDE 48

Lambda Expressions

(Demo)

slide-49
SLIDE 49

Lambda Expressions

12

slide-50
SLIDE 50

Lambda Expressions

>>> x = 10

12

slide-51
SLIDE 51

Lambda Expressions

>>> x = 10 >>> square = x * x

12

slide-52
SLIDE 52

Lambda Expressions

>>> x = 10 >>> square = x * x An expression: this one evaluates to a number

12

slide-53
SLIDE 53

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number

12

slide-54
SLIDE 54

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function

12

slide-55
SLIDE 55

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function A function

12

slide-56
SLIDE 56

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function with formal parameter x A function

12

slide-57
SLIDE 57

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function

12

slide-58
SLIDE 58

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Important: No "return" keyword!

12

slide-59
SLIDE 59

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Important: No "return" keyword! Must be a single expression

12

slide-60
SLIDE 60

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Important: No "return" keyword! Must be a single expression

12

slide-61
SLIDE 61

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Lambda expressions are not common in Python, but important in general Important: No "return" keyword! Must be a single expression

12

slide-62
SLIDE 62

Lambda Expressions

>>> x = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Lambda expressions are not common in Python, but important in general Important: No "return" keyword! Must be a single expression

12

Lambda expressions in Python cannot contain statements at all!

slide-63
SLIDE 63

Lambda Expressions Versus Def Statements

13

slide-64
SLIDE 64

Lambda Expressions Versus Def Statements

VS

13

slide-65
SLIDE 65

Lambda Expressions Versus Def Statements

square = lambda x: x * x

VS

13

slide-66
SLIDE 66

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

13

slide-67
SLIDE 67

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.

13

slide-68
SLIDE 68

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.

13

slide-69
SLIDE 69

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name, which shows up in

environment diagrams but doesn't affect execution (unless the function is printed).

13

slide-70
SLIDE 70

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name, which shows up in

environment diagrams but doesn't affect execution (unless the function is printed).

13

slide-71
SLIDE 71

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name, which shows up in

environment diagrams but doesn't affect execution (unless the function is printed).

13

slide-72
SLIDE 72

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name, which shows up in

environment diagrams but doesn't affect execution (unless the function is printed). The Greek letter lambda

13

slide-73
SLIDE 73

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name, which shows up in

environment diagrams but doesn't affect execution (unless the function is printed). The Greek letter lambda

13

slide-74
SLIDE 74

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name, which shows up in

environment diagrams but doesn't affect execution (unless the function is printed). The Greek letter lambda

13

slide-75
SLIDE 75

Function Composition

(Demo)

slide-76
SLIDE 76

The Environment Diagram for Function Composition

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-77
SLIDE 77

The Environment Diagram for Function Composition

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-78
SLIDE 78

The Environment Diagram for Function Composition

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-79
SLIDE 79

The Environment Diagram for Function Composition

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-80
SLIDE 80

The Environment Diagram for Function Composition

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-81
SLIDE 81

The Environment Diagram for Function Composition

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-82
SLIDE 82

The Environment Diagram for Function Composition

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-83
SLIDE 83

The Environment Diagram for Function Composition

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-84
SLIDE 84

The Environment Diagram for Function Composition

2 1 3

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-85
SLIDE 85

The Environment Diagram for Function Composition

2 1 3

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-86
SLIDE 86

The Environment Diagram for Function Composition

2 1 3 1 2 3

Return value of make_adder is an argument to compose1

15

http://pythontutor.com/composingprograms.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20make_adder%28n%29%3A%0A%20%20%20%20def%20adder%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20k%20%2B%20n%0A%20%20%20%20return%20adder%0A%20%20%20%20%0Adef%20compose1%28f, %20g%29%3A%0A%20%20%20%20def%20h%28x%29%3A%0A%20%20%20%20%20%20%20%20return%20f%28g%28x%29%29%0A%20%20%20%20return%20h%0A%20%20%20%20%0Acompose1%28square,%20make_adder%282%29%29%283%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-87
SLIDE 87

Self-Reference

(Demo)

slide-88
SLIDE 88

Returning a Function Using Its Own Name

17

http://pythontutor.com/composingprograms.html#code=def%20print_all%28k%29%3A%0A%20%20%20%20print%28k%29%0A%20%20%20%20return%20print_all%0A%20%20%20%20%0Aprint_all%281%29%283%29%285%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D http://pythontutor.com/composingprograms.html#code=def%20print_sums%28n%29%3A%0A%20%20%20%20print%28n%29%0A%20%20%20%20def%20next_sum%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20print_sums%28n%2Bk%29%0A%20%20%20%20return%20next_sum%0A%0Aprint_sums%281%29%283%29%285%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-89
SLIDE 89

Currying

slide-90
SLIDE 90

Function Currying

19

slide-91
SLIDE 91

Function Currying

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

19

slide-92
SLIDE 92

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5

19

slide-93
SLIDE 93

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions

19

slide-94
SLIDE 94

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions

19

(Demo)

slide-95
SLIDE 95

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions Curry: Transform a multi-argument function into a single-argument, higher-order function

19

(Demo)