61A-003-Control_1pp.pdf

61A Lecture 3
Wednesday, August 31
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
Call expressions:
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
Number
Call expressions:
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
add
Number
Name
Call expressions:
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
add
'hello'
Number
Name
String
Call expressions:
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
add
'hello'
Number
Name
String
Call expressions:
add
(
2
,
3
)
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
add
'hello'
Number
Name
String
Call expressions:
add
(
2
,
3
)
Operator
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
add
'hello'
Number
Name
String
Call expressions:
add
Operator
(
2
Operand 0
,
3
)
Operand 1
2
Wednesday, August 31, 2011
Lightning Review: Expressions
Primitive expressions:
2
add
'hello'
Number
Name
String
Call expressions:
add
Operator
One big
nested call
expression
(
2
Operand 0
,
3
)
Operand 1
mul(add(2, mul(4, 6)), add(3, 5))
2
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
What happens?
Defining:
>>> def square( x ):
return mul(x, x)
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
What happens?
Defining:
>>> def square( x ):
Def
statement
Call expression:
Calling/Applying:
return mul(x, x)
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
What happens?
Defining:
>>> def square( x ):
Def
statement
Call expression:
Calling/Applying:
return mul(x, x)
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
What happens?
Defining:
>>> def square( x ):
Def
statement
return mul(x, x)
Body
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
What happens?
Defining:
>>> def square( x ):
Def
statement
return mul(x, x)
Body (return statement)
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
What happens?
return mul(x, x)
Body (return statement)
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
What happens?
Function created
return mul(x, x)
Body (return statement)
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Body (return statement)
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
Calling/Applying:
square(2+2)
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Op's evaluated
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Op's evaluated
Function called
square( x ):
return mul(x, x)
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
square(2+2) operand: 2+2
argument: 4
Calling/Applying:
Op's evaluated
Function called
square( x ):
return mul(x, x)
Intrinsic name
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Def
statement
Return
expression
return mul(x, x)
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
square(2+2) operand: 2+2
argument: 4
Calling/Applying:
Op's evaluated
Function called
Signature
square( x ):
return mul(x, x)
Intrinsic name
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Op's evaluated
Function called
Signature
4
square( x ):
return mul(x, x)
Intrinsic name
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Op's evaluated
Function called
Signature
4
square( x ):
return mul(x, x) 16
Intrinsic name
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Op's evaluated
Function called
Signature
4
Argument
square( x ):
return mul(x, x) 16
Intrinsic name
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Op's evaluated
Function called
Signature
4
Argument
Intrinsic name
square( x ):
return mul(x, x) 16
Return value
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Signature
4
Argument
Intrinsic name
square( x ):
Op's evaluated
Function called
New frame!
return mul(x, x) 16
Return value
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Signature
4
Argument
Intrinsic name
square( x ):
return mul(x, x) 16
Op's evaluated
Function called
New frame!
Params bound
Return value
3
Wednesday, August 31, 2011
Life Cycle of a User-Defined Function
Formal parameter
Defining:
>>> def square( x ):
Return
expression
return mul(x, x)
Def
statement
What happens?
Function created
Body stored
Name bound
Body (return statement)
Call expression:
operator: square
function: square
Calling/Applying:
square(2+2) operand: 2+2
argument: 4
Signature
4
Argument
Intrinsic name
square( x ):
return mul(x, x) 16
Op's evaluated
Function called
New frame!
Params bound
Body evaluated
Return value
3
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
a:
2
b:
5
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Name
a:
2
b:
5
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Name
a:
2
b:
5
Value
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
Name
a:
2
b:
5
Value
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Value
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Value
Frame
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Value
A frame is a rectangle
that contains bindings
Frame
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Frame
Value
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environments:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Frame
Value
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environments:
mul:
...
x:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Frame
Value
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environment
Environments:
mul:
...
x:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Frame
Value
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environment
Environments:
mul:
...
An environment is a sequence
of frames
x:
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Frame
Value
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environment
Environments:
mul:
...
x:
An environment is a sequence
of frames
So far, environments only have
at most two frames
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Value
Frame
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environment
Environments:
mul:
...
x:
The global
frame
An environment is a sequence
of frames
So far, environments only have
at most two frames
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Value
Frame
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environment
Environments:
mul:
...
x:
The global
frame
Local
frame
An environment is a sequence
of frames
So far, environments only have
at most two frames
4
Wednesday, August 31, 2011
Cast of Characters: Environment Diagrams
Frames:
Binding
A name is bound to a value
Name
a:
2
b:
5
Value
Frame
A frame is a rectangle
that contains bindings
In a frame, there is at most
one binding per name
Environment
Environments:
mul:
...
x:
The global
frame
Local
frame
An environment is a sequence
of frames
So far, environments only have
at most two frames
(Friday: longer sequences)
4
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Frames link to each other
Local
frame
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the frames that follow
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the frames that follow
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the frames that follow
An environment is a first frame,
plus the sequence of frames that
follow
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Frames link to each other
An environment is a sequence
of frames
Local
frame
An environment is a first frame,
plus the frames that follow
...
An environment is a first frame,
plus the sequence of frames that
follow
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Frames link to each other
An environment is a sequence
of frames
Local
frame
An environment is a first frame,
plus the frames that follow
...
An environment is a first frame,
plus the sequence of frames that
follow
First
frame
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
Frames link to each other
An environment is a sequence
of frames
Local
frame
An environment is a first frame,
plus the frames that follow
...
An environment is a first frame,
plus the sequence of frames that
follow
An environment is a first frame,
plus the environment that follows
First
frame
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
An environment is a sequence
of frames
Local
frame
An environment is a first frame,
plus the frames that follow
...
Extends
Frames link to each other
An environment is a first frame,
plus the sequence of frames that
follow
An environment is a first frame,
plus the environment that follows
First
frame
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
An environment is a sequence
of frames
Local
frame
An environment is a first frame,
plus the frames that follow
...
Existing
environment
Extends
Frames link to each other
An environment is a first frame,
plus the sequence of frames that
follow
An environment is a first frame,
plus the environment that follows
First
frame
5
Wednesday, August 31, 2011
An Environment is a Sequence of Frames
Environments (Memory):
mul:
...
x:
The global
frame
An environment is a sequence
of frames
Local
frame
An environment is a first frame,
plus the frames that follow
...
Existing
environment
Extends
First
frame
Frames link to each other
An environment is a first frame,
plus the sequence of frames that
follow
An environment is a first frame,
plus the environment that follows
New environment
5
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the environment that follows
Expressions (Program):
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the environment that follows
Expressions (Program):
Expressions are Python code
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the environment that follows
Expressions (Program):
Expressions are Python code
square( 2 )
return mul(x, x)
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
x:
The global
frame
Local
frame
Call expression
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the environment that follows
Expressions (Program):
Expressions are Python code
square( 2 )
return mul(x, x)
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
The global
frame
Local
frame
x:
Call expression
Frames link to each other
An environment is a sequence
of frames
An environment is a first frame,
plus the environment that follows
Expressions (Program):
Expressions are Python code
square( 2 )
return mul(x, x)
Return expression
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
The global
frame
An environment is a sequence
of frames
Local
frame
x:
Frames link to each other
An environment is a first frame,
plus the environment that follows
Call expression
Expressions (Program):
Expressions are Python code
square( 2 )
Value
return mul(x, x)
Return expression
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
The global
frame
An environment is a sequence
of frames
Local
frame
x:
Frames link to each other
An environment is a first frame,
plus the environment that follows
Call expression
Expressions (Program):
Expressions are Python code
square( 2 )
Value
Not part of an environment
return mul(x, x)
Return expression
6
Wednesday, August 31, 2011
An Expression is Evaluated in an Environment
Environments (Memory):
mul:
...
The global
frame
An environment is a sequence
of frames
Local
frame
x:
Frames link to each other
An environment is a first frame,
plus the environment that follows
Call expression
Expressions (Program):
Expressions are Python code
square( 2 )
Value
return mul(x, x)
Return expression
Not part of an environment
They are evaluated in an
environment to yield a value
6
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
square:
square(x):
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
square(square(3))
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
square(square(3))
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
square
square(square(3))
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
square
square(square(3))
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
square
square(square(3))
9
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
...
mul(a,b):
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
square
square(square(3))
9
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
mul(a,b):
...
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
x: 9
square
square
square(square(3))
9
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
mul(a,b):
...
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
x: 9
square
square
square(square(3))
9
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Multiple Environments in One Diagram!
mul:
mul(a,b):
...
Every call to a
user-defined function
creates a new
local frame
square:
square(x):
return mul(x, x)
x: 3
x: 9
square
square
81
square(square(3))
9
square(3)
from operator import mul
def square(x):
return mul(x, x)
square(square(3))
7
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
mul:
...
mul(a,b):
square:
square(x):
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
mul:
...
mul(a,b):
square:
square(x):
return mul(x, x)
square(-2)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
...
mul(a,b):
square:
square(x):
return mul(x, x)
square(-2)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
...
mul(a,b):
...in the
earliest frame
of the current
environment
square:
square(x):
return mul(x, x)
square(-2)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
...
mul(a,b):
...in the
earliest frame
of the current
environment
square:
square(x):
return mul(x, x)
...in which that
name is found
square(-2)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
mul(a,b):
...
...in the
earliest frame
of the current
environment
square:
square(x):
square
return mul(x, x)
square(-2)
...in which that
name is found
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
mul(a,b):
...
...in the
earliest frame
of the current
environment
square:
square(x):
square
x: -2
return mul(x, x)
...in which that
name is found
square
square(-2)
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
mul(a,b):
...
...in the
earliest frame
of the current
environment
square:
square(x):
square
x: -2
return mul(x, x)
...in which that
name is found
square
mul
square(-2)
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Names Have No Meaning Without Environments
A name evaluates
to the value
bound to that
name
mul:
mul(a,b):
...
...in the
earliest frame
of the current
environment
square:
square(x):
square
x: -2
return mul(x, x)
...in which that
name is found
square
mul
x
square(-2)
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
8
Wednesday, August 31, 2011
Formal Parameters
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
vs
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
vs
def square(y):
return mul(y, y)
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
vs
def square(y):
return mul(y, y)
Formal parameter
names stay local
to their frame
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
vs
def square(y):
return mul(y, y)
mul:
...
mul(a,b):
square:
Formal parameter
names stay local
to their frame
square(__):
return mul(__, __)
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
vs
def square(y):
return mul(y, y)
mul:
...
Formal parameter
names stay local
to their frame
mul(a,b):
square:
square(__):
return mul(__, __)
from operator import mul
def square(__):
return mul(__, __)
square(-2)
9
Wednesday, August 31, 2011
Formal Parameters
def square(x):
return mul(x, x)
vs
def square(y):
return mul(y, y)
mul:
...
Formal parameter
names stay local
to their frame
mul(a,b):
square:
square(__):
return mul(__, __)
__: -2
square
Local
frame
4
square(-2)
return mul(__,__)
from operator import mul
def square(__):
return mul(__, __)
square(-2)
9
Wednesday, August 31, 2011
Shadowing Names
f
e
r
Ca
ul!
def square(mul):
return mul(mul, mul)
square(-2)
10
Wednesday, August 31, 2011
Shadowing Names
Car
!
l
u
ef
def square(mul):
return mul(mul, mul)
square(-2)
If we use this
formal parameter
10
Wednesday, August 31, 2011
Shadowing Names
Car
!
l
u
ef
def square(mul):
return mul(mul, mul)
square(-2)
If we use this
formal parameter
mul:
...
mul(a,b):
square:
square(mul):
return mul(mul, mul)
mul: -2
square
square(-2)
return mul(mul,mul)
10
Wednesday, August 31, 2011
Shadowing Names
Car
!
l
u
ef
def square(mul):
return mul(mul, mul)
square(-2)
If we use this
formal parameter
mul:
...
mul(a,b):
square:
square(mul):
return mul(mul, mul)
mul: -2
square
square(-2)
Evaluating this call
expression will fail
return mul(mul,mul)
10
Wednesday, August 31, 2011
Shadowing Names
Car
!
l
u
ef
def square(mul):
return mul(mul, mul)
square(-2)
If we use this
formal parameter
mul:
...
mul(a,b):
square:
square(mul):
return mul(mul, mul)
mul: -2
square
square(-2)
Evaluating this call
expression will fail
return mul(mul,mul)
10
Wednesday, August 31, 2011
Python Feature Demonstration
<Demo>
Operators
Multiple Return Values
Docstrings
Doctests
Default Arguments
Statements
</Demo>
11
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
<header>:
<statement>
<statement>
...
<separating header>:
<statement>
<statement>
...
...
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
Statement
<header>:
<statement>
<statement>
...
<separating header>:
<statement>
<statement>
...
...
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
Statement
Clause
<header>:
<statement>
<statement>
...
<separating header>:
<statement>
<statement>
...
...
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
Statement
Clause
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
Statement
Clause
The first header
determines a
statement’s type
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
Statement
Clause
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
The first header
determines a
statement’s type
The header of a clause
“controls” the suite
that follows
12
Wednesday, August 31, 2011
Statements
A statement
is executed by the interpret
to perform an action
Compound statements:
Statement
Clause
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
The first header
determines a
statement’s type
The header of a clause
“controls” the suite
that follows
def statements are
compound statements
12
Wednesday, August 31, 2011
Compound Statements
Compound statements:
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
13
Wednesday, August 31, 2011
Compound Statements
Compound statements:
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
A suite is a sequence
of statements
13
Wednesday, August 31, 2011
Compound Statements
Compound statements:
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
A suite is a sequence
of statements
To “execute” a suite
means to execute its
sequence of statements,
in order
13
Wednesday, August 31, 2011
Compound Statements
Compound statements:
<header>:
<statement>
Suite
<statement>
...
<separating header>:
<statement>
<statement>
...
...
A suite is a sequence
of statements
To “execute” a suite
means to execute its
sequence of statements,
in order
Execution Rule for a sequence of statements:
• Execute the first
• Unless directed otherwise, execute the rest
13
Wednesday, August 31, 2011
Local Assignment
...
percent_difference:
percent_difference(x, y):
...
def percent_difference(x, y):
difference = abs(x-y)
return 100 * difference / x
percent_difference(40, 50)
14
Wednesday, August 31, 2011
Local Assignment
Assignment binds names
in the first frame of
the current environment
...
percent_difference:
percent_difference(x, y):
...
def percent_difference(x, y):
difference = abs(x-y)
return 100 * difference / x
percent_difference(40, 50)
14
Wednesday, August 31, 2011
Local Assignment
Assignment binds names
in the first frame of
the current environment
...
percent_difference:
percent_difference(x, y):
x:
40
y:
50
percent_difference
...
def percent_difference(x, y):
difference = abs(x-y)
return 100 * difference / x
percent_difference(40, 50)
14
Wednesday, August 31, 2011
Local Assignment
Assignment binds names
in the first frame of
the current environment
...
percent_difference:
percent_difference(x, y):
x:
40
y:
50
percent_difference
...
def percent_difference(x, y):
difference = abs(x-y)
return 100 * difference / x
percent_difference(40, 50)
14
Wednesday, August 31, 2011
Local Assignment
Assignment binds names
in the first frame of
the current environment
...
percent_difference:
percent_difference(x, y):
x:
40
y:
50
difference:
10
percent_difference
...
def percent_difference(x, y):
difference = abs(x-y)
return 100 * difference / x
percent_difference(40, 50)
14
Wednesday, August 31, 2011
Conditional Statements
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
elif x == 0:
return 0
else:
return -x
15
Wednesday, August 31, 2011
Conditional Statements
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
1 statement,
elif x == 0:
3 clauses,
return 0
3 headers,
else:
3 suites
return -x
15
Wednesday, August 31, 2011
Conditional Statements
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
1 statement,
elif x == 0:
3 clauses,
return 0
3 headers,
else:
3 suites
return -x
Execution rule for conditional statements:
15
Wednesday, August 31, 2011
Conditional Statements
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
1 statement,
elif x == 0:
3 clauses,
return 0
3 headers,
else:
3 suites
return -x
Execution rule for conditional statements:
Each clause is considered in order.
1. Evaluate the header's expression.
2. If it is a true value, execute the suite & skip the rest.
15
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
elif x == 0:
return 0
else:
return -x
George Boole
16
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
elif x == 0:
return 0
else:
return -x
George Boole
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
False values in Python:
False, 0, ‘’, None
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
False values in Python:
False, 0, ‘’, None
(more to come)
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
False values in Python:
False, 0, ‘’, None
True values in Python:
Anything else (True)
(more to come)
17
Wednesday, August 31, 2011
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x > 0:
return x
Two boolean
elif x == 0:
contexts
return 0
else:
return -x
George Boole
False values in Python:
False, 0, ‘’, None
True values in Python:
Anything else (True)
(more to come)
Read Section 1.5.4!
17
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
0
while i < 3:
1
i = i + 1
total = total + i
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
0
while i < 3:
1
i = i + 1
total = total + i
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
0
while i < 3:
1
i = i + 1
total = total + i
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
Iteration
i, total = 0, 0
i:
while i < 3:
i = i + 1
total:
total = total + i
0
1
2
3
6
Execution rule for while statements:
1. Evaluate the header’s expression.
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
18
Wednesday, August 31, 2011
The Fibonacci Sequence
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
1,
2,
3,
5,
8,
13
,
..
.
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
The Fibonacci Sequence
0,
1,
...
pred:
curr:
1,
2,
3,
5,
8,
13
,
..
.
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
19
Wednesday, August 31, 2011
Project 1: Pig
(Demo)
20
Wednesday, August 31, 2011