DOC PREVIEW
Berkeley COMPSCI 61A - Environments and Higher Order Functions

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 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 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61A Fall 2011 – Akihiro, Stephanie, Tom, Eric K., Eric T., Steven, Aditi, Richard, Hamilton, and Phillip 1 CS61A Notes – Week 3: Environments and Higher Order Functions Expressions Expressions describe a computation and evaluates to a value. Primitive Expressions A primitive expression is a single evaluation step: you either look up the value of a name or take the literal value. For example, numbers, function names and strings are all primitive expressions. >>> 2 2 >>> max <built-in function max> >>> “Hello World” #don’t worry about Strings yet ‘Hello World’ Call Expressions Call expressions are expressions that involve a call to some function. Call expressions are just another type of expression, called a compound expression. The point is in the fact that they are calling some function with arguments and returning some value from them. Recall the syntax of a function call: Evaluation of a function call: 1. Evaluate the operator and operands. 2. Apply the function, (the value of the operator), to the arguments, (the values of the operands). QUESTIONS 1. Determine the result of evaluating f(4) in the Python interpreter if the following functions are defined: from operators import add def double(x): return x + x def square(y): return y * y def f(z): add(square(double(z)), 1) Statements A statement in Python is executed by the interpreter to perform an action. For example, assignment is a statement. We are asking Python to assign a certain value to a variable name, assigning being the action that Python is performing. Assignment is a one-line statement, but we also have the idea of compound statements! A good example is an if statement: if <test>: <true_suite> elif <other_test>: <elif_suite> else: <else_suite>CS61A Fall 2011 – Akihiro, Stephanie, Tom, Eric K., Eric T., Steven, Aditi, Richard, Hamilton, and Phillip 2 Let’s consider how Python evaluates an if statement (it is different than the evaluation of an expression): 1. Evaluate the header's expression. 2. If it is a true value, execute the suite & skip the rest 3. Otherwise, go to the next header (either an elif or an else). If there is no other header, you have finished. 4. If it’s an elif, go back to step 1. Otherwise (if it’s an else) go to step 2 (as if the test was True). Note: Each clause is considered in order. What is a suite? A suite is a sequence of statements or expressions. To evaluate a suite means to execute its sequence of statements or expressions in order. We can generalize and say that compound statements are composed of headers and suites like so: <header>: <statement> <statement> ... <separating-header>: <statement> ... Pure Functions vs. Non-Pure Functions (pay attention to domain and range!) Pure function - ONLY produces a return value (no side effects) and always evaluates to the same result, given the same argument value(s). Non-Pure function - may produce some side effects (like printing to the screen). QUESTIONS 2. What do you think Python will print for the following? Assume this definition of the om and nom procedure: def om(foo): return -foo def nom(foo): print(foo) >>> nom(4) __________ >>> om(-4) __________ >>> save1 = nom(4) >>> save1 + 1 __________ >>> save2 = om(-4) >>> save2 + 1 __________CS61A Fall 2011 – Akihiro, Stephanie, Tom, Eric K., Eric T., Steven, Aditi, Richard, Hamilton, and Phillip 3 Expression Trees Now that we have looked at all of these statements, let’s go all the way back to expressions! Remember, an expression describes a computation and evaluates to a value. And an expression tree helps us to break down an expression into smaller “subexpressions” to understand how they combine with one another. QUESTIONS Draw the expression tree for the following expression calls assuming we have imported the correct functions. 3. add(square(mul(5, 6)), add(mul(1, 2), 7)) 4. (2 + 3 * 4) / 7 5. print(print(abs(-4)))CS61A Fall 2011 – Akihiro, Stephanie, Tom, Eric K., Eric T., Steven, Aditi, Richard, Hamilton, and Phillip 4 First Edition Environment Diagrams We are going to use what is called the “environment model” of evaluation. Environment Diagrams are broken up into two main components: 1. the expression tree 2. environments and values We have already seen expression trees, but what about “environments and values?” Briefly, we keep an “environment” of bindings from variable to value, and every time we encounter a variable, we look up its value in the current environment. Note that we only lookup the value when we see the variable, and so we’ll lookup the value of x only after we’ve already defined x to be 3. An environment frame is a box that contains bindings from variables to values. An environment frame can “extend” another frame; that is, this frame can see all bindings of the frame it extends. We represent this by drawing an arrow from an environment frame to the frame it is extending. The global environment is the only environment that extends nothing. An environment is a series of frames, which we get from extending the current frame. The global environment is only made up of the global frame, because its frame extends nothing. Here is how to go about it: 1. Start with a box (a frame) labeled global environment. This box starts out with bindings for all the built-in functions like +, abs, max, etc. 2. Set the current frame to be the global environment. The current frame is just the environment that you're in at the moment (we will want to keep track of this), and of course, you start in the global environment. 3. Evaluate your expressions, one by one. QUESTIONS Fill out the environment diagram, the environments and values as well as the expression tree, for the following code: 4. >>> def a(n): ... return n + 2 ... >>> def b(m): ... return a(n)/2 ... >>> def e(n): ... return a(b(n * 2) + 1) ... >>> n = 5 >>> e(n) ?? 5. >>> def bar(x): ... return x + x ... >>> def foo(x): ... return bar(add(bar(x),1)) ... >>> garply = foo >>> garply(5) ??CS61A Fall 2011 – Akihiro, Stephanie, Tom, Eric K., Eric T., Steven, Aditi, Richard, Hamilton, and Phillip 5 Some rules to keep in mind when evaluating the following: 1. constants: (numbers, strings, etc), they are self-evaluating so don’t do any work. 2. variables: try to find a


View Full Document

Berkeley COMPSCI 61A - Environments and Higher Order Functions

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 Environments and Higher Order Functions
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 Environments and Higher Order Functions 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 Environments and Higher Order Functions 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?