Fish or Fish?
The Old Proverbs Say
• ‘Give a man a fish and he will eat for a day.
Teach a man how to fish and you feed him for
a lifetime.’
• Chinese:
– 授人以鱼不如授人以渔 A method
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?
• But we can actually write something like this:
Remember how we define a function?
• Almost the same except
– Outside the function distance, you cannot use the
function square
– Just like local variables
Remember how we define a function?
Global
Function
• But we can actually write like this:
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 a variable store a function?!
• Can!!!!!!
Assignments
• The function f is stored in the variable x
– So x is a function, same as f
See the difference
With ‘()’ Without ‘()’
values
types
Functions can be stored in variables
Equivalent
to cos(0)
The type is
“function”
• Can even store functions into a list, tuple, etc.
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
• But useful if you want to return a function as a
result in a function
The “Powerful” Lambda
• Apparently nothing new
Agar Agar (Anyhow) Derivative
• We know that, given a function f, the
derivative of f is
*+(() + ( + ∆( − +(()
= lim
*( ∆"→$ ∆(
• But, if we have very small number dx
*+(() + ( + *( − +(()
≈
*( *(
Agar Agar (Anyhow) Derivative
• We know that, given a function f, the
derivative of f is
*+(() + ( + ∆( − +(()
= lim
*( ∆"→$ ∆(
% &'( "
• = 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
• But, if we have very small number dx
*+(() + ( + *( − +(()
≈
*( *(
Agar Agar (Anyhow) Derivative
• We know that, given a function f, the
derivative of f is
*+(() + ( + ∆( − +(()
= lim
*( ∆"→$ ∆(
• But, if we have very small number dx
*+(() + ( + *( − +(()
≈
*( *(
Agar Agar (Anyhow) Derivative
Take in a function,
returning another
function
Agar Agar (Anyhow) Derivative
% &'( " Derivatives
• cos (
%"
% (" ! *+",-)
• Derivatives
3( / + 3
%"
Example: Newton’s method
• To compute root of function g(x), i.e. find x
such that g(x) = 0
1. Anyhow assume the answer x = something
2. If g(x) ≈ 0 then stop: answer is x, return x
3. Otherwise
• x = x – g(x)/deriv(x)
4. Go to step 2
Example: Newton’s method
• To compute root of function g(x), i.e. find x
such that g(x) = 0
1. Anyhow assume the answer x = something
2. If g(x) ≈ 0 then stop: answer is x, return x
3. Otherwise
• x = x – g(x)/deriv(x)
4. Go to step 2
Example: Newton’s method
• To compute the root of function g(x), i.e. find x
such that g(x) = 0
Example: Newton’s method
• Example: Square root of a number A
– It’s equivalent to solve the equation: ! ! − # = 0
Example: Newton’s method
• Example: Compute log10 (A)
– Solve the equation: 10" − # = 0