cs61a lecture 5
play

CS61A Lecture 5 Amir Kamil UC Berkeley February 1, 2013 - PowerPoint PPT Presentation

CS61A Lecture 5 Amir Kamil UC Berkeley February 1, 2013 Announcements Quiz today! Only worth two points, so dont worry! Hog project Get started early! If you still dont have a partner (and want one), find one on Piazza


  1. CS61A Lecture 5 Amir Kamil UC Berkeley February 1, 2013

  2. Announcements  Quiz today!  Only worth two points, so don’t worry!  Hog project  Get started early!  If you still don’t have a partner (and want one), find one on Piazza  Use existing post; don’t make a new one

  3. The Art of the Function    

  4. The Art of the Function  Give each function exactly one job   

  5. The Art of the Function  Give each function exactly one job  Don’t reapeat yourself (DRY).  

  6. The Art of the Function  Give each function exactly one job  Don’t reapeat yourself (DRY).  Don’t reapeat yourself (DRY). 

  7. The Art of the Function  Give each function exactly one job  Don’t reapeat yourself (DRY).  Don’t reapeat yourself (DRY).  Define functions generally

  8. Generalizing Patterns with Parameters

  9. Generalizing Patterns with Parameters Regular geometric shapes relate length and area.

  10. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape:

  11. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape:

  12. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape:

  13. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape:

  14. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  15. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  16. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  17. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  18. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  19. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  20. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  21. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area:

  22. Generalizing Patterns with Parameters Regular geometric shapes relate length and area. Shape: Area: Finding common structure allows for shared implementation

  23. Generalizing Over Computational Processes

  24. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number.

  25. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number.

  26. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number.

  27. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number.

  28. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number.

  29. Functions as Arguments 

  30. Functions as Arguments Function values can be passed as arguments

  31. Functions as Arguments Function values can be passed as arguments def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

  32. Functions as Arguments Function values can be passed as arguments Function of a single argument (not def cube(k): called term) return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

  33. Functions as Arguments Function values can be passed as arguments Function of a single argument (not def cube(k): called term) return pow(k, 3) A formal parameter that will be def summation(n, term): bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total

  34. Functions as Arguments Function values can be passed as arguments Function of a single argument (not def cube(k): called term) return pow(k, 3) A formal parameter that will be def summation(n, term): bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total The function bound to term gets called here

  35. Functions as Arguments Function values can be passed as arguments Function of a single argument (not def cube(k): called term) return pow(k, 3) A formal parameter that will be def summation(n, term): bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 The cube function is passed as an """ argument value total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total The function bound to term gets called here

  36. Functions as Arguments Function values can be passed as arguments Function of a single argument (not def cube(k): called term) return pow(k, 3) A formal parameter that will be def summation(n, term): bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 The cube function is passed as an """ argument value total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total The function bound to term gets 0 + 1 3 + 2 3 + 3 3 + 4 3 + 5 5 called here

  37. Function Values as Parameters  Example: http://goo.gl/e4YBH

  38. Function Values as Parameters Parameters can be bound to function values Example: http://goo.gl/e4YBH

  39. Function Values as Parameters Parameters can be bound to function values Example: http://goo.gl/e4YBH

  40. Function Values as Parameters Parameters can be bound to function values Example: http://goo.gl/e4YBH

  41. Function Values as Parameters Parameters can be bound to function values Example: http://goo.gl/e4YBH

  42. Function Values as Parameters Parameters can be bound to function values Example: http://goo.gl/e4YBH

  43. Function Values as Parameters Parameters can be bound to function values Example: http://goo.gl/e4YBH

  44. Functions as Return Values   def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

  45. Functions as Return Values Locally defined functions can be returned  def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

  46. Functions as Return Values Locally defined functions can be returned They have access to the frame in which they are defined def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

  47. Functions as Return Values Locally defined functions can be returned They have access to the frame in which they are defined A function that returns a function def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return add(n, k) return adder

  48. Functions as Return Values Locally defined functions can be returned They have access to the frame in which they are defined A function that returns a function def make_adder(n): """Return a function that adds n to its argument. >>> add_three = make_adder(3) >>> add_three(4) 7 """ A local def adder(k): def statement return add(n, k) return adder

  49. Functions as Return Values Locally defined functions can be returned They have access to the frame in which they are defined A function that returns a function def make_adder(n): """Return a function that adds n to its argument. The name add_three is >>> add_three = make_adder(3) bound to a function >>> add_three(4) 7 """ A local def adder(k): def statement return add(n, k) return adder

  50. Functions as Return Values Locally defined functions can be returned They have access to the frame in which they are defined A function that returns a function def make_adder(n): """Return a function that adds n to its argument. The name add_three is >>> add_three = make_adder(3) bound to a function >>> add_three(4) 7 """ A local def adder(k): def statement return add(n, k) return adder Can refer to names in the enclosing function

  51. Call Expressions as Operators make_adder(1)(2) def make_adder(n): def adder(k): return add(n, k) return adder

Recommend


More recommend