61A-003-Control_6pp.pdf

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