Higher-Order Function
: Functions are first-class, meaning they can be manipulated as values
A higher-order function is
1. A function that a function as an argument
2. A function that returns a functionas a return value
Describing Function
: 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
A guide to Designing Function
Give each function exactly one job, but make it apply to many related situations
Generalization
Generalizing patterns with arguements
: Regular geometric shapes relate length and area
Higher-Order Functions
Generalizing Over Computational Process
: The common structure among functions may be a computational process, rather than a number
Functions as Return values
Locally Defined Functions
: Functions defined within other function bodies are bound to names in a local frame
Call Expressions as Operator Expressions
: An expression that evaluates to a function / An expression that evaluates to its argument
Self Reference
Returning a Function using its own name
python codes ex
def print_sums(n) :
print(n)
def next_sum(k) :
return print_sums(n + k)
return next_sum
print_sums(1)(3)(5)
answer. 1 / 4 / 9 / Function
Summary
- Higher-order function : any function that either accepts a function as an argument and / or returns a function
- Why are these useful ?
: Generalize over different form of computation
: Helps remove repetitive segments of code
- One use case: summation
: We generalized over the computation of each term
- We saw nested functions can access variables in outer function (area) as well as the outer function itself (print_sums)
'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Recursion (0) | 2019.07.03 |
---|---|
LAB_day 2 (0) | 2019.07.02 |
Week 2 (0) | 2019.07.02 |
Environment Diagrams (0) | 2019.06.28 |
LAB_day 1 (0) | 2019.06.27 |