This preview shows page 1-2-3-4-5-6-7-8-9-10-11-74-75-76-77-78-79-80-81-82-83-149-150-151-152-153-154-155-156-157-158-159 out of 159 pages.
61A Lecture 2Monday, August 29Monday, August 29, 2011The Elements of Programming2Monday, August 29, 2011The Elements of Programming•Primitive Expressions and StatementsThe simplest building blocks of a language2Monday, August 29, 2011The Elements of Programming•Primitive Expressions and StatementsThe simplest building blocks of a language•Means of CombinationCompound elements are built from simpler ones2Monday, August 29, 2011The Elements of Programming•Primitive Expressions and StatementsThe simplest building blocks of a language•Means of CombinationCompound elements are built from simpler ones•Means of AbstractionCompound elements can be named and manipulated as units2Monday, August 29, 2011The Elements of Programming•Primitive Expressions and StatementsThe simplest building blocks of a language•Means of CombinationCompound elements are built from simpler ones•Means of AbstractionCompound elements can be named and manipulated as units2Programming languages allow us to communicate, tooMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate3Monday, August 29, 2011Functions and DataData: Stuff we want to manipulate3Functions: Rules for manipulating dataMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate32Functions: Rules for manipulating dataMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate32“The Art of Computer Programming”Functions: Rules for manipulating dataMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate32“The Art of Computer Programming”Donald KnuthFunctions: Rules for manipulating dataMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate32“The Art of Computer Programming”Donald KnuthThis slideFunctions: Rules for manipulating dataMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate32“The Art of Computer Programming”Donald KnuthThis slideFunctions: Rules for manipulating dataAdd numbersMonday, August 29, 2011Functions and DataData: Stuff we want to manipulate32“The Art of Computer Programming”Donald KnuthThis slideFunctions: Rules for manipulating dataAdd numbersCount the words in a line of textMonday, August 29, 2011Functions 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 textMonday, August 29, 2011Functions 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 text(Ka-NOOTH)Monday, August 29, 2011Functions 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)Monday, August 29, 2011Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161sin πTypes of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161sin π| − 1869|Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161sin π100�i=1i| − 1869|Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161sin π100�i=1i| − 1869|�6918�Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161sin πf(x)100�i=1i| − 1869|�6918�Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 201118 + 69623√3493161sin πf(x)100�i=1i| − 1869|�6918�Types of expressions4An expressiondescribes a computationand evaluates to a valueMonday, August 29, 2011Call Expressions in PythonAll expressions can use function call notation(Demo)5Monday, August 29, 2011Anatomy of a Call Expression6Monday, August 29, 2011Anatomy of a Call Expression6add ( 2 , 3 )Monday, August 29, 2011Anatomy of a Call Expression6add ( 2 , 3 )OperatorMonday, August 29, 2011Anatomy of a Call Expression6add ( 2 , 3 )Operator Operand 0 Operand 1Monday, August 29, 2011Anatomy of a Call Expression6add ( 2 , 3 )Operator Operand 0 Operand 1Operators and operands are expressionsMonday, August 29, 2011Anatomy of a Call Expression6add ( 2 , 3 )Operator Operand 0 Operand 1Operators and operands are expressionsSo they evaluate to valuesMonday, August 29, 2011Anatomy of a Call Expression6Evaluation procedure for call expressions:add ( 2 , 3 )Operator Operand 0 Operand 1Operators and operands are expressionsSo they evaluate to valuesMonday, August 29, 2011Anatomy 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 subexpressionsSo they evaluate to valuesMonday, August 29, 2011Anatomy 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 valuesMonday, August 29, 2011mul(add(2, mul(4, 6)), add(3, 5))Evaluating Nested Expressions7Monday, August 29, 2011mul(add(2, mul(4, 6)), add(3, 5))Evaluating Nested Expressions7Monday, August 29, 2011mul(add(2, mul(4, 6)), add(3, 5))Evaluating Nested Expressions7mulMonday, August 29, 2011mul(add(2, mul(4, 6)), add(3, 5))add(2, mul(4, 6))Evaluating Nested Expressions7mulMonday, August 29, 2011mul(add(2, mul(4,
View Full Document