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.
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