DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 6

This preview shows page 1-2-3-4-5-6-7-8-52-53-54-55-56-57-58-106-107-108-109-110-111-112-113 out of 113 pages.

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

Unformatted text preview:

61A Lecture 6Friday, September 9Friday, September 9, 2011Lambda Expressions2Friday, September 9, 2011Lambda Expressions2>>> ten = 10Friday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * xFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * xAn expression: this one evaluates to a numberFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberAlso an expression: evaluates to a functionFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberAlso an expression: evaluates to a functionA functionFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberAlso an expression: evaluates to a functionwith formal parameter xA functionFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberAlso an expression: evaluates to a functionand body "return x * x"with formal parameter xA functionFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberAlso an expression: evaluates to a functionand body "return x * x"with formal parameter xA functionNotice: no "return"Friday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * xAn expression: this one evaluates to a numberAlso an expression: evaluates to a functionand body "return x * x"with formal parameter xA functionNotice: no "return"Must be a single expressionFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * x>>> square(4)16An expression: this one evaluates to a numberAlso an expression: evaluates to a functionand body "return x * x"with formal parameter xA functionNotice: no "return"Must be a single expressionFriday, September 9, 2011Lambda Expressions2>>> ten = 10>>> square = x * x>>> square = lambda x: x * x>>> square(4)16An expression: this one evaluates to a numberAlso an expression: evaluates to a functionand body "return x * x"with formal parameter xA functionLambda expressions are rare in Python, but important in generalNotice: no "return"Must be a single expressionFriday, September 9, 2011Lambda Expressions Versus Def Statements3Friday, September 9, 2011Lambda Expressions Versus Def Statements3VSFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xVSFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVSFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVS• Both create a function with the same arguments & bodyFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVS• Both create a function with the same arguments & body• Both of those functions are associated with the environment in which they are definedFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVS• Both create a function with the same arguments & body• Both of those functions are associated with the environment in which they are defined• Both bind that function to the name "square"Friday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVS• Both create a function with the same arguments & body• Both of those functions are associated with the environment in which they are defined• Both bind that function to the name "square"• Only the def statement gives the function an intrinsic nameFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVS• Both create a function with the same arguments & body• Both of those functions are associated with the environment in which they are defined• Both bind that function to the name "square"• Only the def statement gives the function an intrinsic name<lambda>(x):return x * xFriday, September 9, 2011Lambda Expressions Versus Def Statements3square = lambda x: x * xdef square(x): return x * xVS• Both create a function with the same arguments & body• Both of those functions are associated with the environment in which they are defined• Both bind that function to the name "square"• Only the def statement gives the function an intrinsic name<lambda>(x):return x * xsquare(x):return x * xFriday, September 9, 2011Function Currying4Friday, September 9, 2011Function Currying4def make_adder(n): return lambda k: n + kFriday, September 9, 2011Function Currying4def make_adder(n): return lambda k: n + k>>> make_adder(2)(3)5>>> add(2, 3)5Friday, September 9, 2011Function Currying4def make_adder(n): return lambda k: n + k>>> make_adder(2)(3)5>>> add(2, 3)5There's a general relationship between these functionsFriday, September 9, 2011Function Currying4def make_adder(n): return lambda k: n + k>>> make_adder(2)(3)5>>> add(2, 3)5There's a general relationship between these functionsCurrying: Transforming a multi-argument function into a single-argument, higher-order function.Friday, September 9, 2011Function Currying4def make_adder(n): return lambda k: n + k>>> make_adder(2)(3)5>>> add(2, 3)5There's a general relationship between these functionsCurrying: Transforming a multi-argument function into a single-argument, higher-order function.Fun Fact: Currying was discovered by Moses Schönfinkel and later re-discovered by Haskell Curry.Friday, September 9, 2011Function Currying4def make_adder(n): return lambda k: n + k>>> make_adder(2)(3)5>>> add(2, 3)5There's a general relationship between these functionsCurrying: Transforming a multi-argument function into a single-argument, higher-order function.Fun Fact: Currying was discovered by Moses Schönfinkel and later re-discovered by Haskell Curry.Schönfinkeling?Friday, September 9, 2011Newton's Method BackgroundFinds approximations to zeroes of differentiable functions 5Friday, September 9, 2011Newton's Method


View Full Document

Berkeley COMPSCI 61A - Lecture 6

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 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 6
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 6 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 6 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?