The Elements of Programming Primitive Expressions and Statements! The simplest building blocks of a language 61 Lecture Monday, ugust 9 Means of Combination! Compound elements are built from simpler ones Means of bstraction! Compound elements can be named and manipulated as units Programming languages allow us to communicate, too Functions and Data Types of expressions Data: Stuff we want to manipulate The rt of Computer Programming This slide Donald Knuth (Ka-NOOTH) Functions: Rules for manipulating data Count the words in a line of text dd numbers Load the next slide Pronounce someone s name 18 + 69 f(x) 1869 n expression describes a computation and evaluates to a value 6 sin π 3 393161 100 i i=1 69 18 3 Call Expressions in Python natomy of a Call Expression add (, 3 ) Operator Operand 0 Operand 1 Operators and operands are expressions ll expressions can use function call notation So they evaluate to values Evaluation procedure for call expressions: 1. Evaluate the operator and operand subexpressions. pply the function that is the value of the operator subexpression to the arguments that are the values of the operand subexpression 5 6
Evaluating Nested Expressions The Print Function 08 mul(add(, mul(, 6)), add(3, 5)) mul 6 add(, mul(, 6)) 8 add(3, 5) add mul(, 6) add 3 5 mul 6 7 8 Pure Functions & Non-Pure Functions Pure Functions -, 100 Function signature: how many parameters abs(number): pow(x, y): Only produces return values 16765060089019670305376 Nested Expressions with Print, display print(print(1), print()) Non-Pure Functions print print(1) print() - May create side effects 1 display - display 1 display 9 10 Names and ssignment Built-in function abs: abs(x): Imported value ssigned value max: max(a, b, c, ): pi: 3.1 tau: 6.8 from math import pi tau = * pi The environment does not track where names came from 11 1
User-Defined Functions frame holds name bindings abs: abs(x): max: max(a, b, c, ): Named values are a simple means of abstraction Named expressions are a more powerful means of abstraction def expressions: Create a new function pi: 3.1 tau: 6.8 Binding to a numeric value Bind a name to it from math import pi tau = * pi The environment does not track where names came from >>> def <name>(<formal parameters>): return <return expression> 13 1 User-Defined Functions Named values are a simple means of abstraction Named expressions are a more powerful means of abstraction mul(a,b): def expressions: Create a new function Bind a name to it mul(a,b): >>> def def 15 16 Bind formal parameters mul(a,b): Eval return expression x: - (-) & Values Expressions def (-) The environment created for the function body The existing environment in which the call expression is evaluated (-) def (-) 17 18
Evaluating a Name in an Environment mul(a,b): mul(a,b): name evaluates to the value bound to that name x: - (-) def (-) x: - Points to an environment, which starts with a frame in the earliest frame of the current environment in which that name is found 19 0 Intrinsic Function Names Don t Play a Role mul(a,b): Does not get inspected sum_s: add, mul, sum_s(x, y): return add((x), (y)) x: - Witness Protection Program Functions aren t accessed by their intrinsic names! def def sum_s(x, y): return add((x),(y)) 1 sum_s: add, mul, sum_s: add, mul, y: 1 sum_s(x, y): return add((x), (y)) y: 1 sum_s(x, y): return add((x), (y)) sum_s sum_s sum_s(5,1) sum_s(5,1) add((x),(y)) add((x),(y)) return def def sum_s(x,y): return add() sum_s(5, 1) 3 def def sum_s(x,y): return add() sum_s(5, 1)
sum_s: add, mul, sum_s: add, mul, y: 1 sum_s sum_s(5,1) sum_s(x, y): add((x),(y)) 5 (x) mul(x, x) return add((x), (y)) def def sum_s(x,y): return add() sum_s(5, 1) 5 y: 1 sum_s x: 1 sum_s(5,1) sum_s(x, y): 169 add((x),(y)) (x) 5 return add((x), (y)) 1 (y) mul(x, x) mul(x, x) def def sum_s(x,y): return add() sum_s(5, 1) 6