DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 2

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

61A Lecture 2Monday, August 29The Elements of Programming•Primitive Expressions and Statements!The simplest building blocks of a language•Means of Combination!Compound elements are built from simpler ones•Means of Abstraction!Compound elements can be named and manipulated as units2Programming languages allow us to communicate, tooFunctions and DataData: Stuff we want to manipulate32“The Art of Computer Programming”Donald KnuthThis slideFunctions: Rules for manipulating dataAdd numbersPronounce someone’s nameCount the words in a line of textLoad the next slide(Ka-NOOTH)18 + 69623√3493161sin πf(x)100�i=1i| − 1869|�6918�Types of expressions4An expressiondescribes a computationand evaluates to a valueCall Expressions in PythonAll expressions can use function call notation(Demo)5Anatomy of a Call Expression6Evaluation procedure for call expressions:add ( 2 , 3 )Operator Operand 0 Operand 1Operators and operands are expressions1. Evaluate the operator and operand subexpressions2. Apply the function that is the value of the operator subexpression to the arguments that are the values of the operand subexpressionSo they evaluate to values208mul(add(2, mul(4, 6)), add(3, 5))add(2, mul(4, 6))Evaluating Nested Expressions726muladd 2mul(4, 6)mul 4 624add(3, 5)add 3 58The Print Function(Demo)8Pure Functions & Non-Pure Functions9-22-2Noneabs(number):print(...):display “-2”2, 1001267650600228229401496703205376pow(x, y):May createside effectsOnly produces return valuesPure FunctionsNon-Pure FunctionsFunction signature: how many parametersNested Expressions with Print10Noneprint(print(1), print(2))printprint(...):1Nonedisplay “1”print(...):2Nonedisplay “2”print(...):None, NoneNonedisplay “None None”print(1)Noneprint(2)NoneNames and Assignment(Demo)11abs:abs(x):max:...max(a, b, c, ...):...Environments12from math import pitau = 2 * pipi:3.14...tau:6.28...Built-in functionImported valueAssigned valueThe environment does not track where names came fromabs:abs(x):max:...max(a, b, c, ...):...Environments13from math import pitau = 2 * pipi:3.14tau:6.28The environment does not track where names came fromBinding to a numeric valueA frame holds name bindingsUser-Defined FunctionsNamed values are a simple means of abstractionNamed expressions are a more powerful means of abstraction14<name>(<formal parameters>):return <return expression>>>> defdef expressions:• Create a new function• Bind a name to it...mul:mul(a,b):User-Defined FunctionsNamed values are a simple means of abstractionNamed expressions are a more powerful means of abstraction15square(x):return mul(x, x)>>> defsquare:def expressions:• Create a new function• Bind a name to it(Demo)Calling User-Defined Functions16square:square(x):return mul(x, x)...from operator import mul def square(x): return mul(x, x)mul:mul(a,b):squaresquare(-2)Calling User-Defined Functions17mul:x: -2square:square(x):return mul(x, x)4return mul(x, x)...mul(a,b):Environments & ValuesExpressionsfrom operator import mul def square(x): return mul(x, x)square(-2)• Bind formal parameters• Eval return expressionsquare(-2)Calling User-Defined Functions184return mul(x, x)from operator import mul def square(x): return mul(x, x)square(-2)The environmentcreated for the function bodyThe existing environment in which the call expression is evaluatedsquaresquare(-2)Calling User-Defined Functions19mul:x: -2square:square(x):return mul(x, x)4return mul(x, x)...mul(a,b):from operator import mul def square(x): return mul(x, x)square(-2)squareEvaluating a Name in an Environment20mul:x: -2square:square(x):return mul(x, x)return mul(x, x)...mul(a,b):Points to an environment, which starts with a frameA name evaluates to the value bound to that name...in the earliest frame of the current environment...in which that name is foundIntrinsic Function Names Don’t Play a Role21squaremul:x: -2square:square(x):return mul(x, x)return mul(x, x)...mul(a,b):Does not get inspectedWitness Protection ProgramFunctions aren’t accessed by their intrinsic names!(Demo)Example: Function Application22sum_squares:square(x):return mul(x, x)def square(x): return mul(x,x)def sum_squares(x, y): return add(square(x),square(y))sum_squares(x, y):return add(square(x), square(y))square:add, mul, ...sum_squares(5,12)sum_squaresExample: Function Application23sum_squares:square(x):return mul(x, x)def square(x): return mul(x,x)def sum_squares(x,y): return add(...)sum_squares(5, 12)sum_squares(x, y):return add(square(x), square(y))square:add, mul, ...add(square(x),square(y))x: 5y: 12returnExample: Function Application24sum_squares:square(x):return mul(x, x)def square(x): return mul(x,x)def sum_squares(x,y): return add(...)sum_squares(5, 12)sum_squares(x, y):return add(square(x), square(y))square:add, mul, ...sum_squares(5,12)add(square(x),square(y))x: 5y: 12sum_squaresAAExample: Function Application25sum_squares:square(x):return mul(x, x)def square(x): return mul(x,x)def sum_squares(x,y): return add(...)sum_squares(5, 12)sum_squares(x, y):return add(square(x), square(y))square:add, mul, ...sum_squares(5,12)add(square(x),square(y))x: 5y: 12sum_squaresAAsquarex: 5square(x)mul(x, x)A25squaresum_squares(5,12)add(square(x),square(y))169square(y)144Example: Function Application26sum_squares:square(x):return mul(x, x)def square(x): return mul(x,x)def sum_squares(x,y): return add(...)sum_squares(5, 12)sum_squares(x, y):return add(square(x), square(y))square:add, mul, ...x: 5y: 12sum_squaressquare(x)AAA Ax: 5x: 12square25mul(x, x) mul(x,


View Full Document

Berkeley COMPSCI 61A - Lecture 2

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?