Office Hours: You Should Go! You are not alone! 61A Lecture 5 Wednesday, September 7 http://inst.eecs.berkeley.edu/~cs61a/fa11/www/staff.html 2 Reminder: Multiple Assignment & Return Values The Structure of Project 1 Page 1 Two functions implement the game simulation 61Aé003éControl_div.py Integer division, Integer remainder which rounds down after dividing """Functions for exact integer division.""" def play(...): from operator import floordiv, mod Warning! Pseudo-code (not code) while game is not over: def divide_exact(n, d): """Return the quotient and remainder of dividing n by d. get a plan (from the current player's strategy) call take_turn with a dice and plan >>> q, r = divide_exact(13, 5) >>> q 2 Multiple assignment >>> r to two names 3 """ return floordiv(n, d), mod(n, d) return winner def take_turn(...): while turn is not over: get an action (from plan) and outcome (from dice) Multiple return values, separated by commas call an action return points scored during the turn 3 The Structure of Project 1 The Purpose of Higher-Order Functions Functions are first-class: Functions can be manipulated as values in our programming language. Four types of functions are involved in simulating game Domain Range Action (integer, integer) Plan integer (integer, integer, boolean) Action Strategy (integer, integer) Plan No arguments integer Two arguments Dice 4 Higher-order function: A function that takes a function as an argument value or returns a function as a return value Three return values Higher-order functions: 5 • Express general methods of computation • Remove repetition from programs • Separate concerns among functions 6 hof.py Page 2 return total Review: Summation Example def identity(k): return k Environments Enable Higher-Order Functions! Function of a single argument (not called term) def cube(k): return pow(k, 3) Functions as arguments: Our current environment model handles that! A formal parameter that def summation(n, term):will be bound to a function """Sum the first n terms of a sequence. We'll give an example of how >>> 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 Functions as return values: We need to extend the model a little Functions need to know where they were defined The function bound to term Almost everything stays the same gets called here def pi_term(k): return 8 / (k * 4 é 3) / (k * 4 é 1) 0 + 13 + 23 + 33 + 43 + 55 7 8 # Local function definitions; returning functions def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) Names7and Environments with Functional Values """ def adder(k): return k + n apply_twice: return adder Applying User-Defined Functions apply_twice(f, x): def square: compose1(f, g): return f(f(x)) """Return a square(x): function that composes f and g. square: ... The first frame of the environment in which the function was defined square(x): return x * x return x * x f: x: 2 f, g éé functions of a single argument """ def apply_twice(f, x): def h(x): return f(f(x)) return f(g(x)) 16 return h def square(x): apply_twice(square, 2) 2 square 4 return x * x return f(f(x)) @main def run(): 4 interact() square 16 x: -2 square(-2) apply_twice(square, 2) square return x * x def square(x): return x * x square(-2) 4 hof.py 9 Pag return total 10 def identity(k): return k def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) Functions Associated with the Global Frame 225 Locally """ Defined Functions: Example This is the global frame square: ... Associated with the global frame total, k = 0, 1 while k <= n: total, k = total + term(k), + 1 function bodies Functions defined within kother return total are bound to names in the local frame def pi_term(k): return 8 / (k * 4 é 3) / (k * 4 é 1) A function that returns a function square(x): return x * x # Local function definitions; returning functions def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) The name add_three is >>> add_three(4) bound to a function 7 """ def adder(k): A local return k + n def statement return adder x: -2 square 4 square(-2) return x * x def square(x): return x * x square(-2) def compose1(f, g): """Return a function that composes f and g. Can refer to names in 11 the enclosing function f, g éé functions of a single argument """ def h(x): return f(g(x)) return h @main def run(): interact() 12 Locally Defined Functions: Call Expressions Locally Defined Functions: Environments make_adder: make_adder(1)(2) make_adder(1) ( Operator 2 n: 1 ) def make_adder(n): def adder(k): return k + n return adder make_adder(1)(2) adder: Operand 0 An expression that evaluates to a function make_adder(n): ... make_adder adder(k): return k + n k: 2 An expression that evaluates to any value Associated with a local frame adder def make_adder(n): def adder(k): return k + n return adder make_adder(1)(2) n Apply adder to 2 3 make_adder(1)(2) return k + n make_adder(1) 13 The Environment for Function Composition compose1: make_adder: a1: a2: ... make_adder(n): ... n: 1 adder: make_adder k: 3 adder >>> square = x * x Notice: no "return" A function with formal parameter x and body "return x * x" adder: x: 3 h f: g: h: compose1 Also an expression: evaluates to a function >>> square = lambda x: x * x (Demo) n: 2 make_adder An expression: this one evaluates to a number >>> ten = 10 return k + n k: 5 adder Lambda Expressions def compose1(f, g): def h(x) return f(g(x)) return h a1 = make_adder(1) a2 = make_adder(2) compose1(a1, a2)(3) adder(k): 14 >>> square(4) 16 adder(k): Must be a single expression return k + n Lambda expressions are rare in Python, but important in general h(x): return f(g(x)) 15 More Higher-Order Function Examples (Demo) 17 16
© Copyright 2026 Paperzz