61a lecture 6
play

61A Lecture 6 Friday, September 13 Announcements 2 Announcements - PowerPoint PPT Presentation

61A Lecture 6 Friday, September 13 Announcements 2 Announcements Homework 2 due Tuesday 9/17 @ 11:59pm 2 Announcements Homework 2 due Tuesday 9/17 @ 11:59pm Project 2 due Thursday 9/19 @ 11:59pm 2 Announcements Homework 2 due


  1. 61A Lecture 6 Friday, September 13

  2. Announcements 2

  3. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm 2

  4. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm 2

  5. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm • Optional Guerrilla section next Monday for students to master higher-order functions 2

  6. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm • Optional Guerrilla section next Monday for students to master higher-order functions  Organized by Andrew Huang and the readers 2

  7. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm • Optional Guerrilla section next Monday for students to master higher-order functions  Organized by Andrew Huang and the readers  Work in a group on a problem until everyone in the group understands the solution 2

  8. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm • Optional Guerrilla section next Monday for students to master higher-order functions  Organized by Andrew Huang and the readers  Work in a group on a problem until everyone in the group understands the solution • Midterm 1 on Monday 9/23 from 7pm to 9pm 2

  9. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm • Optional Guerrilla section next Monday for students to master higher-order functions  Organized by Andrew Huang and the readers  Work in a group on a problem until everyone in the group understands the solution • Midterm 1 on Monday 9/23 from 7pm to 9pm  Details and review materials will be posted early next week 2

  10. Announcements • Homework 2 due Tuesday 9/17 @ 11:59pm • Project 2 due Thursday 9/19 @ 11:59pm • Optional Guerrilla section next Monday for students to master higher-order functions  Organized by Andrew Huang and the readers  Work in a group on a problem until everyone in the group understands the solution • Midterm 1 on Monday 9/23 from 7pm to 9pm  Details and review materials will be posted early next week  There will be a web form for students who cannot attend due to a conflict 2

  11. Lambda Expressions (Demo)

  12. Lambda Expressions 4

  13. Lambda Expressions >>> ten = 10 4

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

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

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

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

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

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

  20. 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 that returns the value of "x * x" 4

  21. 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 Important : No "return" keyword! A function with formal parameter x that returns the value of "x * x" 4

  22. 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 Important : No "return" keyword! A function with formal parameter x that returns the value of "x * x" Must be a single expression 4

  23. 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 Important : No "return" keyword! A function with formal parameter x that returns the value of "x * x" >>> square(4) 16 Must be a single expression 4

  24. 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 Important : No "return" keyword! A function with formal parameter x that returns the value of "x * x" >>> square(4) 16 Must be a single expression Lambda expressions are not common in Python, but important in general 4

  25. 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 Important : No "return" keyword! A function with formal parameter x that returns the value of "x * x" >>> square(4) 16 Must be a single expression Lambda expressions are not common in Python, but important in general Lambda expressions in Python cannot contain statements at all! 4

  26. Lambda Expressions Versus Def Statements 5 Example: http://goo.gl/XH54uE

  27. Lambda Expressions Versus Def Statements VS 5 Example: http://goo.gl/XH54uE

  28. Lambda Expressions Versus Def Statements VS square = lambda x: x * x 5 Example: http://goo.gl/XH54uE

  29. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x 5 Example: http://goo.gl/XH54uE

  30. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. 5 Example: http://goo.gl/XH54uE

  31. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. 5 Example: http://goo.gl/XH54uE

  32. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. • Both bind that function to the name square . 5 Example: http://goo.gl/XH54uE

  33. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. • Both bind that function to the name square . • Only the def statement gives the function an intrinsic name. 5 Example: http://goo.gl/XH54uE

  34. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. • Both bind that function to the name square . • Only the def statement gives the function an intrinsic name. 5 Example: http://goo.gl/XH54uE

  35. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. • Both bind that function to the name square . • Only the def statement gives the function an intrinsic name. 5 Example: http://goo.gl/XH54uE

  36. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. • Both bind that function to the name square . • Only the def statement gives the function an intrinsic name. The Greek letter lambda 5 Example: http://goo.gl/XH54uE

  37. Lambda Expressions Versus Def Statements def square(x): VS square = lambda x: x * x return x * x • Both create a function with the same domain, range, and behavior. • Both functions have as their parent the environment in which they were defined. • Both bind that function to the name square . • Only the def statement gives the function an intrinsic name. The Greek letter lambda 5 Example: http://goo.gl/XH54uE

  38. Currying

  39. Function Currying 7

  40. Function Currying def make_adder(n): return lambda k: n + k 7

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

Recommend


More recommend