61A Lecture 2 Announcements Names, Assignment, and User-Defined - - PowerPoint PPT Presentation

61a lecture 2 announcements names assignment and user
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 2 Announcements Names, Assignment, and User-Defined - - PowerPoint PPT Presentation

61A Lecture 2 Announcements Names, Assignment, and User-Defined Functions (Demo) Types of Expressions 4 Types of Expressions Primitive expressions: 4 Types of Expressions Primitive expressions: 2 Number or Numeral 4 Types of


slide-1
SLIDE 1

61A Lecture 2

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Names, Assignment, and User-Defined Functions

(Demo)

slide-4
SLIDE 4

Types of Expressions

4

slide-5
SLIDE 5

Types of Expressions

Primitive expressions:

4

slide-6
SLIDE 6

Types of Expressions

Primitive expressions: 2 Number or Numeral

4

slide-7
SLIDE 7

Types of Expressions

Primitive expressions: 2 add Number or Numeral Name

4

slide-8
SLIDE 8

Types of Expressions

Primitive expressions: 2 add 'hello' Number or Numeral Name String

4

slide-9
SLIDE 9

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' Number or Numeral Name String

4

slide-10
SLIDE 10

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' max ( 2 , 3 ) Number or Numeral Name String

4

slide-11
SLIDE 11

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' max ( 2 , 3 ) Operator Number or Numeral Name String

4

slide-12
SLIDE 12

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' max ( 2 , 3 ) Operator Operand Operand Number or Numeral Name String

4

slide-13
SLIDE 13

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' max ( 2 , 3 ) Operator Operand Operand max(min(pow(3, 5), -4), min(1, -2)) Number or Numeral Name String

4

slide-14
SLIDE 14

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' max ( 2 , 3 ) Operator Operand Operand max(min(pow(3, 5), -4), min(1, -2)) Number or Numeral Name String

4

An operand can also be a call expression

slide-15
SLIDE 15

Types of Expressions

Primitive expressions: Call expressions: 2 add 'hello' max ( 2 , 3 ) Operator Operand Operand max(min(pow(3, 5), -4), min(1, -2)) Number or Numeral Name String

4

An operand can also be a call expression

slide-16
SLIDE 16

Discussion Question 1

5

slide-17
SLIDE 17

Discussion Question 1

What is the value of the final expression in this sequence?

5

slide-18
SLIDE 18

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min

5

slide-19
SLIDE 19

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min >>> f = max

5

slide-20
SLIDE 20

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min >>> f = max >>> g, h = min, max

5

slide-21
SLIDE 21

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min >>> f = max >>> g, h = min, max >>> max = g

5

slide-22
SLIDE 22

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min >>> f = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1, 5), 3)), 4)

5

slide-23
SLIDE 23

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min >>> f = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1, 5), 3)), 4)

???

5

slide-24
SLIDE 24

Discussion Question 1

What is the value of the final expression in this sequence? >>> f = min >>> f = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1, 5), 3)), 4)

???

5

slide-25
SLIDE 25

Environment Diagrams

slide-26
SLIDE 26

Environment Diagrams

Environment diagrams visualize the interpreter’s process.

7

Interactive Diagram

slide-27
SLIDE 27

Environment Diagrams

Environment diagrams visualize the interpreter’s process.

7

Interactive Diagram

slide-28
SLIDE 28

Environment Diagrams

Environment diagrams visualize the interpreter’s process.

7

Interactive Diagram

slide-29
SLIDE 29

Environment Diagrams

Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Interactive Diagram

slide-30
SLIDE 30

Environment Diagrams

Statements and expressions Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Interactive Diagram

slide-31
SLIDE 31

Environment Diagrams

Import statement Statements and expressions Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Interactive Diagram

slide-32
SLIDE 32

Environment Diagrams

Import statement Statements and expressions Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Assignment statement Interactive Diagram

slide-33
SLIDE 33

Environment Diagrams

Import statement Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Assignment statement Interactive Diagram

slide-34
SLIDE 34

Environment Diagrams

Import statement Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Assignment statement Interactive Diagram

slide-35
SLIDE 35

Environment Diagrams

Import statement Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Next to execute Assignment statement Interactive Diagram

slide-36
SLIDE 36

Environment Diagrams

Import statement Each name is bound to a value Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Next to execute Assignment statement Interactive Diagram

slide-37
SLIDE 37

Environment Diagrams

Name Import statement Each name is bound to a value Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Next to execute Assignment statement Interactive Diagram

slide-38
SLIDE 38

Environment Diagrams

Name Value Import statement Each name is bound to a value Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Next to execute Assignment statement Interactive Diagram

slide-39
SLIDE 39

Environment Diagrams

Name Value Import statement Each name is bound to a value Within a frame, a name cannot be repeated Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Next to execute Assignment statement Interactive Diagram

slide-40
SLIDE 40

Environment Diagrams

(Demo) Name Value Import statement Each name is bound to a value Within a frame, a name cannot be repeated Statements and expressions Arrows indicate evaluation order Frames (right): Code (left): Environment diagrams visualize the interpreter’s process.

7

Just executed Next to execute Assignment statement Interactive Diagram

slide-41
SLIDE 41

Assignment Statements

8

Interactive Diagram

slide-42
SLIDE 42

Assignment Statements

8

Interactive Diagram

slide-43
SLIDE 43

Assignment Statements

8

Interactive Diagram

slide-44
SLIDE 44

Assignment Statements

8

Just executed Interactive Diagram

slide-45
SLIDE 45

Assignment Statements

8

Just executed Next to execute Interactive Diagram

slide-46
SLIDE 46

Assignment Statements

Execution rule for assignment statements:

8

Just executed Next to execute Interactive Diagram

slide-47
SLIDE 47

Assignment Statements

Execution rule for assignment statements:

  • 1. Evaluate all expressions to the right of = from left to right.

8

Just executed Next to execute Interactive Diagram

slide-48
SLIDE 48

Assignment Statements

Execution rule for assignment statements:

  • 1. Evaluate all expressions to the right of = from left to right.
  • 2. Bind all names to the left of = to those resulting values in the current frame.

8

Just executed Next to execute Interactive Diagram

slide-49
SLIDE 49

Assignment Statements

Execution rule for assignment statements:

  • 1. Evaluate all expressions to the right of = from left to right.
  • 2. Bind all names to the left of = to those resulting values in the current frame.

8

Just executed Just executed Next to execute Interactive Diagram

slide-50
SLIDE 50

Assignment Statements

Execution rule for assignment statements:

  • 1. Evaluate all expressions to the right of = from left to right.
  • 2. Bind all names to the left of = to those resulting values in the current frame.

8

Just executed Just executed Next to execute Interactive Diagram

slide-51
SLIDE 51

Discussion Question 1 Solution

9

(Demo) Interactive Diagram

slide-52
SLIDE 52

Discussion Question 1 Solution

9

(Demo) Interactive Diagram

slide-53
SLIDE 53

Discussion Question 1 Solution

9

(Demo) Interactive Diagram

slide-54
SLIDE 54

Discussion Question 1 Solution

9

(Demo) Interactive Diagram

slide-55
SLIDE 55

Discussion Question 1 Solution

9

func min(...) (Demo) Interactive Diagram

slide-56
SLIDE 56

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) (Demo) Interactive Diagram

slide-57
SLIDE 57

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) func max(...) 2 (Demo) Interactive Diagram

slide-58
SLIDE 58

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) func max(...) 2 (Demo) Interactive Diagram

slide-59
SLIDE 59

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) func max(...) 2 func min(...) (Demo) Interactive Diagram

slide-60
SLIDE 60

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) func max(...) 2 h(1, 5) func min(...) (Demo) Interactive Diagram

slide-61
SLIDE 61

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) func max(...) 2 h(1, 5) func min(...) 5 func max(...) 1 (Demo) Interactive Diagram

slide-62
SLIDE 62

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) func max(...) 2 h(1, 5) func min(...) 5 5 func max(...) 1 (Demo) Interactive Diagram

slide-63
SLIDE 63

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) func max(...) 2 3 h(1, 5) func min(...) 5 5 func max(...) 1 (Demo) Interactive Diagram

slide-64
SLIDE 64

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) g(h(1, 5), 3) 3 func max(...) 2 3 h(1, 5) func min(...) 5 5 func max(...) 1 (Demo) Interactive Diagram

slide-65
SLIDE 65

Discussion Question 1 Solution

9

func min(...) f(2, g(h(1, 5), 3)) 3 g(h(1, 5), 3) 3 func max(...) 2 3 h(1, 5) func min(...) 5 5 func max(...) 1 (Demo) Interactive Diagram

slide-66
SLIDE 66

Discussion Question 1 Solution

9

func min(...) 4 f(2, g(h(1, 5), 3)) 3 g(h(1, 5), 3) 3 func max(...) 2 3 h(1, 5) func min(...) 5 5 func max(...) 1 (Demo) Interactive Diagram

slide-67
SLIDE 67

Discussion Question 1 Solution

9

func min(...) 4 3 f(2, g(h(1, 5), 3)) 3 g(h(1, 5), 3) 3 func max(...) 2 3 h(1, 5) func min(...) 5 5 func max(...) 1 (Demo) Interactive Diagram

slide-68
SLIDE 68

Discussion Question 1 Solution

9

func min(...) 4 3 f(2, g(h(1, 5), 3)) 3 g(h(1, 5), 3) 3 func max(...) 2 3 h(1, 5) func min(...) 5 5 func max(...) 1

3

(Demo) Interactive Diagram

slide-69
SLIDE 69

Defining Functions

slide-70
SLIDE 70

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions

11

slide-71
SLIDE 71

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def

11

slide-72
SLIDE 72

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def Function signature indicates how many arguments a function takes

11

slide-73
SLIDE 73

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def Function signature indicates how many arguments a function takes Function body defines the computation performed when the function is applied

11

slide-74
SLIDE 74

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def Execution procedure for def statements: Function signature indicates how many arguments a function takes Function body defines the computation performed when the function is applied

11

slide-75
SLIDE 75

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def Execution procedure for def statements:

  • 1. Create a function with signature <name>(<formal parameters>)

Function signature indicates how many arguments a function takes Function body defines the computation performed when the function is applied

11

slide-76
SLIDE 76

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def Execution procedure for def statements:

  • 1. Create a function with signature <name>(<formal parameters>)
  • 2. Set the body of that function to be everything indented after the first line

Function signature indicates how many arguments a function takes Function body defines the computation performed when the function is applied

11

slide-77
SLIDE 77

Defining Functions

Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions <name>(<formal parameters>): return <return expression> >>> def Execution procedure for def statements:

  • 1. Create a function with signature <name>(<formal parameters>)
  • 2. Set the body of that function to be everything indented after the first line
  • 3. Bind <name> to that function in the current frame

Function signature indicates how many arguments a function takes Function body defines the computation performed when the function is applied

11

slide-78
SLIDE 78

Calling User-Defined Functions

12

Interactive Diagram

slide-79
SLIDE 79

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

12

Interactive Diagram

slide-80
SLIDE 80

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment

12

Interactive Diagram

slide-81
SLIDE 81

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame

12

Interactive Diagram

slide-82
SLIDE 82

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

12

Interactive Diagram

slide-83
SLIDE 83

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

12

Interactive Diagram

slide-84
SLIDE 84

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Built-in function

12

Interactive Diagram

slide-85
SLIDE 85

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Built-in function User-defined function

12

Interactive Diagram

slide-86
SLIDE 86

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Local frame Built-in function User-defined function

12

Interactive Diagram

slide-87
SLIDE 87

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Local frame Original name of function called Built-in function User-defined function

12

Interactive Diagram

slide-88
SLIDE 88

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Local frame Original name of function called Formal parameter bound to argument Built-in function User-defined function

12

Interactive Diagram

slide-89
SLIDE 89

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Local frame Original name of function called Formal parameter bound to argument Return value
 (not a binding!) Built-in function User-defined function

12

Interactive Diagram

slide-90
SLIDE 90

Calling User-Defined Functions

13

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Interactive Diagram

slide-91
SLIDE 91

Calling User-Defined Functions

A function’s signature has all the information needed to create a local frame

13

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Interactive Diagram

slide-92
SLIDE 92

Calling User-Defined Functions

A function’s signature has all the information needed to create a local frame

13

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Interactive Diagram

slide-93
SLIDE 93

Calling User-Defined Functions

A function’s signature has all the information needed to create a local frame

13

Procedure for calling/applying user-defined functions (version 1):

  • 1. Add a local frame, forming a new environment
  • 2. Bind the function's formal parameters to its arguments in that frame
  • 3. Execute the body of the function in that new environment

Interactive Diagram

slide-94
SLIDE 94

Looking Up Names In Environments

14

slide-95
SLIDE 95

Looking Up Names In Environments

Every expression is evaluated in the context of an environment.

14

slide-96
SLIDE 96

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

14

slide-97
SLIDE 97

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or

14

slide-98
SLIDE 98

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

14

slide-99
SLIDE 99

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day:

14

slide-100
SLIDE 100

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day: An environment is a sequence of frames.

14

slide-101
SLIDE 101

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found.

14

slide-102
SLIDE 102

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. E.g., to look up some name in the body of the square function:

14

slide-103
SLIDE 103

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. E.g., to look up some name in the body of the square function:

  • Look for that name in the local frame.

14

slide-104
SLIDE 104

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. E.g., to look up some name in the body of the square function:

  • Look for that name in the local frame.
  • If not found, look for it in the global frame.


(Built-in names like “max” are in the global frame too, 
 but we don’t draw them in environment diagrams.)

14

slide-105
SLIDE 105

Looking Up Names In Environments

Every expression is evaluated in the context of an environment. So far, the current environment is either:

  • The global frame alone, or
  • A local frame, followed by the global frame.

Most important two things I’ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. E.g., to look up some name in the body of the square function:

  • Look for that name in the local frame.
  • If not found, look for it in the global frame.


(Built-in names like “max” are in the global frame too, 
 but we don’t draw them in environment diagrams.) (Demo)

14