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