This preview shows page 1-2-3-4-5-6-7-8-53-54-55-56-57-58-59-107-108-109-110-111-112-113-114 out of 114 pages.
61A Lecture 5Wednesday, September 7Wednesday, September 7, 2011Office Hours: You Should Go!You are not alone!2Wednesday, September 7, 2011Office Hours: You Should Go!You are not alone!2Wednesday, September 7, 2011Office Hours: You Should Go!You are not alone!2http://inst.eecs.berkeley.edu/~cs61a/fa11/www/staff.htmlWednesday, September 7, 2011Reminder: Multiple Assignment & Return Values361Aé003éControl_div.py Page 1"""Functions for exact integer division."""from operator import floordiv, moddef divide_exact(n, d): """Return the quotient and remainder of dividing n by d. >>> q, r = divide_exact(13, 5) >>> q 2 >>> r 3 """ return floordiv(n, d), mod(n, d)Wednesday, September 7, 2011Reminder: Multiple Assignment & Return Values361Aé003éControl_div.py Page 1"""Functions for exact integer division."""from operator import floordiv, moddef divide_exact(n, d): """Return the quotient and remainder of dividing n by d. >>> q, r = divide_exact(13, 5) >>> q 2 >>> r 3 """ return floordiv(n, d), mod(n, d)Integer division, which rounds downWednesday, September 7, 2011Reminder: Multiple Assignment & Return Values361Aé003éControl_div.py Page 1"""Functions for exact integer division."""from operator import floordiv, moddef divide_exact(n, d): """Return the quotient and remainder of dividing n by d. >>> q, r = divide_exact(13, 5) >>> q 2 >>> r 3 """ return floordiv(n, d), mod(n, d)Integer division, which rounds downInteger remainderafter dividing Wednesday, September 7, 2011Reminder: Multiple Assignment & Return Values361Aé003éControl_div.py Page 1"""Functions for exact integer division."""from operator import floordiv, moddef divide_exact(n, d): """Return the quotient and remainder of dividing n by d. >>> q, r = divide_exact(13, 5) >>> q 2 >>> r 3 """ return floordiv(n, d), mod(n, d)Multiple return values, separated by commasInteger division, which rounds downInteger remainderafter dividing Wednesday, September 7, 2011Reminder: Multiple Assignment & Return Values361Aé003éControl_div.py Page 1"""Functions for exact integer division."""from operator import floordiv, moddef divide_exact(n, d): """Return the quotient and remainder of dividing n by d. >>> q, r = divide_exact(13, 5) >>> q 2 >>> r 3 """ return floordiv(n, d), mod(n, d)Multiple return values, separated by commasMultiple assignmentto two namesInteger division, which rounds downInteger remainderafter dividing Wednesday, September 7, 2011The Structure of Project 14Two functions implement the game simulationWednesday, September 7, 2011The Structure of Project 14Two functions implement the game simulationWarning!Pseudo-code(not code)Wednesday, September 7, 2011The Structure of Project 14def play(...): while game is not over: get a plan (from the current player's strategy) call take_turn with a dice and plan return winnerTwo functions implement the game simulationWarning!Pseudo-code(not code)Wednesday, September 7, 2011The Structure of Project 14def play(...): while game is not over: get a plan (from the current player's strategy) call take_turn with a dice and plan return winnerdef take_turn(...): while turn is not over: get an action (from plan) and outcome (from dice) call an action return points scored during the turnTwo functions implement the game simulationWarning!Pseudo-code(not code)Wednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeWednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeAction (integer, integer) (integer, integer, boolean)Wednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeAction (integer, integer) (integer, integer, boolean)Two argumentsWednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeAction (integer, integer) (integer, integer, boolean)Two arguments Three return valuesWednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeAction (integer, integer) (integer, integer, boolean)Planinteger ActionTwo arguments Three return valuesWednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeAction (integer, integer) (integer, integer, boolean)Planinteger ActionStrategy(integer, integer) PlanTwo arguments Three return valuesWednesday, September 7, 2011The Structure of Project 15Four types of functions are involved in simulating gameDomain RangeAction (integer, integer) (integer, integer, boolean)Planinteger ActionStrategy(integer, integer) PlanDiceNo arguments integerTwo arguments Three return valuesWednesday, September 7, 2011The Purpose of Higher-Order Functions6Wednesday, September 7, 2011The Purpose of Higher-Order FunctionsFunctions are first-class: Functions can be manipulated as values in our programming language.6Wednesday, September 7, 2011The Purpose of Higher-Order FunctionsFunctions are first-class: Functions can be manipulated as values in our programming language.6Higher-order function: A function that takes a function as an argument value or returns a function as a return valueWednesday, September 7, 2011The Purpose of Higher-Order FunctionsFunctions are first-class: Functions can be manipulated as values in our programming language.6Higher-order functions:Higher-order function: A function that takes a function as an argument value or returns a function as a return valueWednesday, September 7, 2011The Purpose of Higher-Order FunctionsFunctions are first-class: Functions can be manipulated as values in our programming language.6Higher-order functions:• Express general methods of computationHigher-order function: A function that takes a function as an argument value or returns a function as a return valueWednesday, September 7, 2011The Purpose of Higher-Order FunctionsFunctions are first-class: Functions can be manipulated as values in our programming language.6Higher-order functions:• Express general methods of computation•
View Full Document