ITP 20005 Programming Languages Chapter 1 1.6 Higher-Order Functions Major references: 1. Structure and Interpretation of Computer Programs(SICP) by Abelson and Sussman, MIT Composing Programs by John DeNero, Google 2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia and many on-line resources. Prof. Youngsup Kim, [email protected], 2014 Programming Languages, CSEE Dept., Handong Global University
32
Embed
ITP 20005 Programming Languages Chapter 1 1.6 Higher-Order …contents.kocw.net/KOCW/document/2014/handong/kimyoungsup/... · 2016-09-09 · ITP 20005 Programming Languages Chapter
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Major references: 1. Structure and Interpretation of Computer Programs(SICP) by Abelson and Sussman, MIT
Composing Programs by John DeNero, Google2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia and many on-line resources.Prof. Youngsup Kim, [email protected], 2014 Programming Languages, CSEE Dept., Handong Global University
compose1(f, g)(3)f = squareg = make_adder(2) make_adder(2) returns adder which is adder(k) and has n = 2. compose1(f, g) returns h which is h(x) = f(g(x))
h(3) = square(adder(3))= square(5)
5
1.6 HOF: Environment for Function Composition
6
1.6.2 Example: Functions as General Methods
Challenge: Implement a general method for iterative improvement to compute the golden ratio "phi" which is near 1.618.
http://www.youtube.com/watch?v=AzfHbETDbN8
Fibonacci and Golden ratio(mean)
7
1.6.2 Example: Functions as General Methods
Challenge: Implement a general method for iterative improvement to compute the golden ratio "phi" which is near 1.618.
Mat 7:12 So in everything, do to others
what you would have them do to you,
for this sums up the Law and the Prophets.
그러므로 무엇이든지 남에게 대접을 받고자 하는 대로 너희도 남을 대접하라 이것이율법이요 선지자니라.
8
1.6.2 Example: Functions as General Methods
Challenge: Implement a general method for iterative improvement to compute the golden ratio "phi" which is near 1.618.
How do you get this? HW?
9
1.6.2 Example: Functions as General Methods
Challenge: Implement a general method for iterative improvement to compute the golden ratio "phi" which is near 1.618.
𝑎 + 𝑏
𝑎= 1 +
𝑏
𝑎= 1 +
1
𝜑≡ 𝜑
s𝑖𝑛𝑐𝑒 1 +1
𝜑= 𝜑, 𝑡ℎ𝑒𝑛 1 + 𝜑 = 𝜑2
𝜑2 − 𝜑 − 1 = 0
∴ 𝜑 =1 + 5
2
10
1.6.2 Example: Functions as General Methods
Challenge: Implement a general method for iterative improvement to compute the golden ratio "phi" which is near 1.618.
11
1.6.2 Example: Functions as General Methods
Challenge: Implement a general method for iterative improvement to compute the golden ratio "phi" which is near 1.618.
1. Begin with a guess of a solution to an equation.
2. It repeatedly applies an update function to improve that guess.
3. Apply a close comparison to check whether current guess is close enough to end.
def improve(update, close, guess = 1):while not close(guess):
guess = update(guess)return guess
function
def update(guess): # get the next guessreturn 1 + 1/guess # 1 + 1/r = r by definition
Begin with a function 𝑓(𝑥) and an initial guess 𝑥0.
𝑥𝑛
20
1.6.5 Example: Using Newton’s Method
How to find the square root of 2?
>>> f = lambda x: x*x - 2
>>> find_zero(f)
1.4142135623730951 𝑓 𝑥 = 𝑥2 − 2
How to find the log base of 1024?
>>> g = lambda x: pow(2, x) – 1024
>>> find_zero(f)
10.0 𝑔 𝑥 = 2𝑥 − 1024
21
1.6.5 Example: Using Newton’s Method
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a.
Update: 𝑥 = 𝑥 −𝒇 𝒙
𝒇′ 𝒙= 𝑥 −
𝒙𝟐−𝑎
2𝑥=
𝑥+𝑎
𝑥
2
initial guessnext guess
𝒇 𝒙 = 𝒙𝟐 − 𝒂
𝒇′ 𝒙 = 𝟐𝒙
22
1.6.5 Example: Using Newton’s Method
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a.
Update: 𝑥 =𝑥+
𝑎
𝑥
2
𝒙 – 𝒇(𝒙)/𝒇’(𝒙)
𝑩𝒂𝒃𝒚𝒍𝒐𝒏𝒊𝒂𝒏𝑴𝒆𝒕𝒉𝒐𝒅
Implementation questions:
next guess
initial guess
23
1.6.5 Example: Using Newton’s Method
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a.
Update: 𝑥 =𝑥+
𝑎
𝑥
2
𝒙 – 𝒇(𝒙)/𝒇’(𝒙)
𝑩𝒂𝒃𝒚𝒍𝒐𝒏𝒊𝒂𝒏𝑴𝒆𝒕𝒉𝒐𝒅
Implementation questions:
• What guess should start the computation?
• How do we know when we are finished?
24
1.6.5 Example: Square Roots – Special Case
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a.
Demo:>>> def square_root(a):... x = 1... while x*x != a:... x = (x + a/x) / 2... return x>>> square_root(4)2.0>>> square_root(2)Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 4, in square_root
KeyboardInterrupt
Why not working?
It never reached the right number since the limitation of the floating point number representation of the computer.
25
1.6.5 Example: Square Roots – Special Case
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a.