61a lecture 6
play

61A Lecture 6 Friday, September 7 Lambda Expressions 2 Lambda - PowerPoint PPT Presentation

61A Lecture 6 Friday, September 7 Lambda Expressions 2 Lambda Expressions >>> ten = 10 2 Lambda Expressions >>> ten = 10 >>> square = x * x 2 Lambda Expressions An expression: this one >>> ten = 10


  1. 61A Lecture 6 Friday, September 7

  2. Lambda Expressions 2

  3. Lambda Expressions >>> ten = 10 2

  4. Lambda Expressions >>> ten = 10 >>> square = x * x 2

  5. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x 2

  6. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x >>> square = lambda x: x * x 2

  7. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x 2

  8. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x A function 2

  9. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x A function with formal parameter x 2

  10. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x A function with formal parameter x and body "return x * x" 2

  11. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x Notice: no "return" A function with formal parameter x and body "return x * x" 2

  12. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x Notice: no "return" A function with formal parameter x and body "return x * x" Must be a single expression 2

  13. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x Notice: no "return" A function with formal parameter x and body "return x * x" >>> square(4) 16 Must be a single expression 2

  14. Lambda Expressions An expression: this one >>> ten = 10 evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x Notice: no "return" A function with formal parameter x and body "return x * x" >>> square(4) 16 Must be a single expression Lambda expressions are rare in Python, but important in general 2

  15. Lambda Expressions Versus Def Statements 3

  16. Lambda Expressions Versus Def Statements VS 3

  17. Lambda Expressions Versus Def Statements VS square = lambda x: x * x 3

  18. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x 3

  19. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior 3

  20. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • Both of those functions are associated with the environment in which they are defined 3

  21. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • Both of those functions are associated with the environment in which they are defined • Both bind that function to the name "square" 3

  22. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • 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 3

  23. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • 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 3

  24. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • 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 3

  25. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • 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 3

  26. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same arguments & behavior • 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 The Greek letter lambda 3

  27. Function Currying 4

  28. Function Currying def make_adder(n): return lambda k: n + k 4

  29. Function Currying def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 4

  30. Function Currying def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 There's a general >>> add(2, 3) relationship between 5 these functions 4

  31. Function Currying def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 There's a general >>> add(2, 3) relationship between 5 these functions Currying: Transforming a multi-argument function into a single-argument, higher-order function. 4

  32. Function Currying def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 There's a general >>> add(2, 3) relationship between 5 these functions Currying: 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. 4

  33. Function Currying def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 There's a general >>> add(2, 3) relationship between 5 these functions Currying: 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? 4

  34. Newton's Method Background Finds approximations to zeroes of differentiable functions 5

  35. Newton's Method Background Finds approximations to zeroes of differentiable functions f(x) = x 2 - 2 5

  36. Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2 - 2 -5 -2.5 0 2.5 5 -2.5 5

  37. Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2 - 2 A "zero" -5 -2.5 0 2.5 5 -2.5 5

  38. Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2 - 2 A "zero" -5 -2.5 0 2.5 5 x=1.414213562373095 -2.5 5

  39. Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2 - 2 A "zero" -5 -2.5 0 2.5 5 x=1.414213562373095 -2.5 Application: a method for (approximately) computing square roots, using only basic arithmetic. 5

  40. Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2 - 2 A "zero" -5 -2.5 0 2.5 5 x=1.414213562373095 -2.5 Application: a method for (approximately) computing square roots, using only basic arithmetic. The positive zero of f(x) = x 2 - a is 5

  41. Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2 - 2 A "zero" -5 -2.5 0 2.5 5 x=1.414213562373095 -2.5 Application: a method for (approximately) computing square roots, using only basic arithmetic. The positive zero of f(x) = x 2 - a is � √ 5

  42. Newton's Method Begin with a function f and an initial guess x � − ��� ) ����� 6

  43. Newton's Method Begin with a function f and an initial guess x � − ��� ) ����� 6

  44. Newton's Method Begin with a function f and an initial guess x 1. Compute the value of f at the guess: f(x) � − ��� ) ����� 6

Recommend


More recommend