Lecture 4 Higher-Order Functions
- Questions
- When you draw an environment diagram, is the parent function when you call the function or when you define the function?
- Example: Prime Factorization (Review)
- Each positive integer n has a set of primate factors: primes whos product is n
- A possible way to solve
- Keep dividing by smallest factor until reach 1
- Using functions helps keep code readable
- Example: Iteration (Review)
- The Fibonacci Sequence
- Multi-line assignment
- Calculate each value on the right side
- Apply values to left side
- Multi-line assignment
- The Fibonacci Sequence
- Control and Call Expressions
- Boolean Contexts
- What is the boolean value of an expression?
- We only care if it is truthy or falsey
- Examples: x<0, x==0
- If Statements and Call Expressions
- Execution rule for Conditional Statements
- Evaluate header’s expression
- If it is a true value, execute the suite and skip remaining clauses
- Trying to recreate if function
- if(’header expression’, ‘if suite’, ‘else suite’)
- Evaluation Rule for Call Expression
- Evaluate operator and operand subexpressions
- Code errors, because program tries to evaluate parameter n//d
- Evaluate operator and operand subexpressions
- Execution rule for Conditional Statements
- Boolean Contexts
- Higher-Order Functions
- Generalizing Over Computational Processes
- Common structure among functions may be a computational process, rather than a number
- Generalizable Function
- Can have a general function that does summation
- Use a term function to do an operation on k
- Generalizing Over Computational Processes
- Types of Higher-Order Functions
- Environments Enable Higher-Order Functions
- Higher-order functions
- A function that takes a function as an argument value
- A function that returns a function as a return value
- first class
- Functions are values in our programming language
- Higher-order functions
- Names can be bound to Functional Arguments
- Applying a user-defined function
- Create new frame
- bind formal parameters to arguments
- Execute the body
- Applying a user-defined function
- Environments Enable Higher-Order Functions
- Functions as Return Values
- Can return a function to generalize
- Locally Defined Functions
- Functions defined within other function bodies are bound to names in a local frame
- Can refer to names in the enclosing function
- Environment Diagrams for Nested Def Statements
- Look at parent function before global
- Every user-defined function has a parent frame (often global)
- Parent of a frame is the parent of the function called
- Call Expressions as Operator Expressions
- make_adder(1)(2)
- make_adder(1) is actually the operator
- expression evaluates to a function
- (2) is the operand
- make_adder(1) is actually the operator
- make_adder(1)(2)
- How to Draw an Environment Diagram
- When a function is defined
- Its parent is the current frame
- Bind name to the function value in the current frame
- When a function is called
- Add a local frame, titled with the
of the function being called - Copy the parent of the function to the local frame
- based on the function definition
- Bind the formal parameters to the arguments in the local frame
- Execute the body of the function in the environment that starts with the local frame
- Add a local frame, titled with the
- When a function is defined
- Local Names
- Local Names are not Visible to Other (Non-Nested) Functions
- function g is defined in global frame
- cannot access function f’s frame
- function g is defined in global frame
- Local Names are not Visible to Other (Non-Nested) Functions
- Function Composition
-
Lambda Expressions
- Another way to make a function
lambda x: x**2 square = lambda x: x**2
- square = x*x (expression that evaluates to a number)
- square = lambda x: x*x (also an expression: evaluates to a function)
- def vs lambda statement
- def statement doesn’t return anything
- lambda returns a function value
- def vs lambda statement
- Syntax
- lambda - Function
- x - with formal parameter x
- xx - returns the value of “xx”
- Lambda expressions cannot contain statements at all
- Useful for inputs into other functions
- faster way than def to make functions
- Lambda Expressions vs def statements
- Both create a function with same domain, range, behavior
- Both bind function tothe name square
- Only the def statement gives the function an intrinsic name
- shows up in environment diagrams, but doesn’t affect execution
- Put line number for lambda, as there can be different lambda functions
- Def functions don’t need line number because there can only be one name of that
- Higher Order functions with Lambdas
- Lambdas can return another lambda
- Summary
- Functional abstraction
- use logic before
- Well defined functions can help redundancy in our code
- high-order functions → input/output functions
- Functions can be nested within other functions
- Control structures have different evaluation than functions
- Lambda expressions are a quick way to define simple functions within a single line