Higher-order Functions 9/30/2019 Slides adapted from Berkeley CS61a
Higher-Order Functions 9 / 30 / 2019 Slides adapted from Berkeley CS61a
Higher-Order Functions Functions are first-class,meaning they can be manipulated as values A higher-order function is: 1.A function that takes a function as an argument and/or 1.A function that returns a function as a return value
Higher-Order Functions Functions are first-class, meaning they can be manipulated as values A higher-order function is: 1. A function that takes a function as an argument and/or 1. A function that returns a function as a return value
Designing Functions
Designing Functions
Describing Functions def square(x): """Return X *x""" A function's domain is the set of all inputs it might x is a number possibly take as arguments. A function's range is the set of output values it square returns a non- might possibly return. negative real number A pure function's behavior is the relationship it square returns the square of x creates between input and output
Describing Functions A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. def square(x): """Return X * X""" x is a number square returns a nonnegative real number square returns the square of x
A Guide to Designing Function Give each function exactly one job,but make it apply to many related situations >>>round(1.23) >>round(1.23,1) >>round(1.23,0) >>round(1.23,5) 1 1.2 1 1.23 Don't repeat yourself (DRY).Implement a process just once,but execute it many times
A Guide to Designing Function Give each function exactly one job, but make it apply to many related situations >>> round(1.23) 1 >>> round(1.23, 1) 1.2 >>> round(1.23, 0) 1 >>> round(1.23, 5) 1.23 Don’t repeat yourself (DRY). Implement a process just once, but execute it many times
Generalization
Generalization
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: 0 Area: 0r2 元2 3v3) 2 Finding common structure allows for shared implementation Demo
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: Area: Finding common structure allows for shared implementation Demo
Higher-Order Functions
Higher-Order Functions
Generalizing Over Computational Processes The common structure among functions may be a computational process,rather than a number. 5 =1+2+3+4+5 =15 k=1 5 =13+23+3+43+53 =225 k=1 51 =- 8 8 8 8 8 3+ =3.04 35 99 195 + 323 Demo
Generalizing Over Computational Processes The common structure among functions may be a computational process, rather than a number. Demo
Summation Example Function of a single argument def cube(k): (not called "term") return pow(k,3) A formal parameter that will def summation(n,term) be bound to a function """Sum the first n terms of a sequence. >>summation(5,fcube) 225) n鞋 The cube function is passed 0+1+8+27+64+125 total,k =0,1 as an argument value while k <n: total,k total +'term(k),k 1 return total The function bound to term gets called here
Summation Example 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 Function of a single argument (not called "term") A formal parameter that will be bound to a function The cube function is passed 0 + 1 + 8 + 27 + 64 + 125 as an argument value The function bound to term gets called here