lec11_Higher_Order_Functions
lec11_Higher_Order_Functions
An object
Mr. X
‘fish’
A variable x = …
A function that
produce ‘fish’
Mr. X
A variable x = A function
return ‘fish’
Mr. X
Higher Order Functions
Remember how we define a function?
Global
Function
Local
Function
Treat a Function like a Variable
“Callability”
• Normal variables are NOT callable
• A function is callable
Assignments
• Normal variables can store values
• Can!!!!!!
Assignments
• The function f is stored in the variable x
– So x is a function, same as f
See the difference
values
types
Functions can be stored in variables
Equivalent
to cos(0)
The type is
“function”
Equivalent to
len([1,2,3])
Function Composition
• In math, we can do something like
log(sin ( )
Equivalent to
Mix and Match
A function
A variable
(can be a
function
too!)
Equivalent to
Mr. X
The Evil Lambda
The Big Evil Boss “lambda”
Equivalent!!!
• difference:
– lambda does not need a ‘return’
The “Powerful” Lambda
• Apparently nothing new
% &'( "
• = cos (
%"
% (" ! *+",-)
• = 3( / + 3
%"
Agar Agar (Anyhow) Derivative
• We know that, given a function f, the
derivative of f is
*+(() + ( + ∆( − +(()
= lim
*( ∆"→$ ∆(
% &'( "
• Derivatives
cos (
%"
% (" ! *+",-)
• Derivatives
3( / + 3
%"
Agar Agar (Anyhow) Derivative
• We know that, given a function f, the
derivative of f is
*+(() + ( + ∆( − +(()
Take in a function,
= lim returning another
*( ∆"→$ ∆( function
Take in a function,
returning another
function
Agar Agar (Anyhow) Derivative