cs61a lecture 7 - university of california,...
TRANSCRIPT
![Page 1: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/1.jpg)
CS61A Lecture 7
Amir KamilUC Berkeley
February 6, 2013
![Page 2: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/2.jpg)
HW3 out, due Tuesday at 7pmMidterm next Wednesday at 7pm
Keep an eye out for your assigned location Old exams posted soon Review sessions
Saturday 2‐4pm in TBA Extend office hours Sunday 11‐3pm in TBA HKN review session Sunday 3‐6pm in 145 Dwinelle
Environment diagram handout on website Code review system online
See Piazza post for details
Announcements
![Page 3: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/3.jpg)
How to Draw an Environment Diagram
![Page 4: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/4.jpg)
How to Draw an Environment Diagram
When defining a function:
![Page 5: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/5.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
![Page 6: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/6.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
![Page 7: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/7.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
Bind <name> to the function value in the first frame of the current environment
![Page 8: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/8.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
Bind <name> to the function value in the first frame of the current environment
When calling a function:
![Page 9: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/9.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
Bind <name> to the function value in the first frame of the current environment
When calling a function:
1. Add a local frame labeled with the <name> of the function
![Page 10: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/10.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
Bind <name> to the function value in the first frame of the current environment
When calling a function:
1. Add a local frame labeled with the <name> of the function
2. If the function has a parent label, copy it to this frame
![Page 11: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/11.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
Bind <name> to the function value in the first frame of the current environment
When calling a function:
1. Add a local frame labeled with the <name> of the function
2. If the function has a parent label, copy it to this frame
3. Bind the <formal parameters> to the arguments in this frame
![Page 12: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/12.jpg)
How to Draw an Environment Diagram
When defining a function:
Create a function value with signature <name>(<formal parameters>)
For nested definitions, label the parent as the first frame of the current environment
Bind <name> to the function value in the first frame of the current environment
When calling a function:
1. Add a local frame labeled with the <name> of the function
2. If the function has a parent label, copy it to this frame
3. Bind the <formal parameters> to the arguments in this frame
4. Execute the body of the function in the environment that starts with this frame
![Page 13: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/13.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 14: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/14.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 15: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/15.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 16: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/16.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 17: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/17.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 18: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/18.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 19: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/19.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 20: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/20.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 21: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/21.jpg)
Environment for Function Composition
Example: http://goo.gl/5zcug
![Page 22: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/22.jpg)
Environment for Function Composition
2
1
3
Example: http://goo.gl/5zcug
![Page 23: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/23.jpg)
Environment for Function Composition
2
1
3
Example: http://goo.gl/5zcug
![Page 24: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/24.jpg)
Environment for Function Composition
2
1
3
Example: http://goo.gl/5zcug
![Page 25: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/25.jpg)
Environment for Function Composition
2
1
3
1
2
3
Example: http://goo.gl/5zcug
![Page 26: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/26.jpg)
Lambda Expressions
![Page 27: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/27.jpg)
Lambda Expressions
>>> ten = 10
![Page 28: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/28.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
![Page 29: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/29.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
An expression: this one evaluates to a number
![Page 30: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/30.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
![Page 31: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/31.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
Also an expression: evaluates to a function
![Page 32: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/32.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
Also an expression: evaluates to a function
A function
![Page 33: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/33.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
Also an expression: evaluates to a function
with formal parameter xA function
![Page 34: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/34.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
Also an expression: evaluates to a function
and body "return x * x"with formal parameter x
A function
![Page 35: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/35.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
Also an expression: evaluates to a function
and body "return x * x"with formal parameter x
A functionNotice: no "return"
![Page 36: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/36.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
An expression: this one evaluates to a number
Also an expression: evaluates to a function
and body "return x * x"with formal parameter x
A functionNotice: no "return"
Must be a single expression
![Page 37: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/37.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
>>> square(4)16
An expression: this one evaluates to a number
Also an expression: evaluates to a function
and body "return x * x"with formal parameter x
A functionNotice: no "return"
Must be a single expression
![Page 38: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/38.jpg)
Lambda Expressions
>>> ten = 10
>>> square = x * x
>>> square = lambda x: x * x
>>> square(4)16
An expression: this one evaluates to a number
Also an expression: evaluates to a function
and body "return x * x"with formal parameter x
A function
Lambda expressions are rare in Python, but important in general
Notice: no "return"
Must be a single expression
![Page 39: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/39.jpg)
Evaluation of Lambda vs. Def
![Page 40: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/40.jpg)
Evaluation of Lambda vs. Def
lambda x: x * x
![Page 41: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/41.jpg)
Evaluation of Lambda vs. Def
lambda x: x * x VS
![Page 42: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/42.jpg)
Evaluation of Lambda vs. Def
lambda x: x * xdef square(x):
return x * xVS
![Page 43: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/43.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:
lambda x: x * xdef square(x):
return x * xVS
![Page 44: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/44.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:1. Create a function value with signature
<name>(<formal parameters>)and the current frame as parent
lambda x: x * xdef square(x):
return x * xVS
![Page 45: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/45.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:1. Create a function value with signature
<name>(<formal parameters>)and the current frame as parent
2. Bind <name> to that value in the current frame
lambda x: x * xdef square(x):
return x * xVS
![Page 46: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/46.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:1. Create a function value with signature
<name>(<formal parameters>)and the current frame as parent
2. Bind <name> to that value in the current frame
Evaluation procedure for lambda expressions:
lambda x: x * xdef square(x):
return x * xVS
![Page 47: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/47.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:1. Create a function value with signature
<name>(<formal parameters>)and the current frame as parent
2. Bind <name> to that value in the current frame
Evaluation procedure for lambda expressions:1. Create a function value with signature
(<formal parameters>)and the current frame as parent
lambda x: x * xdef square(x):
return x * xVS
![Page 48: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/48.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:1. Create a function value with signature
<name>(<formal parameters>)and the current frame as parent
2. Bind <name> to that value in the current frame
Evaluation procedure for lambda expressions:1. Create a function value with signature
(<formal parameters>)and the current frame as parent
lambda x: x * xdef square(x):
return x * xVS
![Page 49: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/49.jpg)
Evaluation of Lambda vs. Def
Execution procedure for def statements:1. Create a function value with signature
<name>(<formal parameters>)and the current frame as parent
2. Bind <name> to that value in the current frame
Evaluation procedure for lambda expressions:1. Create a function value with signature
(<formal parameters>)and the current frame as parent
2. Evaluate to that value
lambda x: x * xdef square(x):
return x * xVS
![Page 50: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/50.jpg)
Lambda vs. Def Statements
![Page 51: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/51.jpg)
Lambda vs. Def Statements
square = lambda x: x * x
![Page 52: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/52.jpg)
Lambda vs. Def Statements
square = lambda x: x * x VS
![Page 53: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/53.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVS
![Page 54: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/54.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
![Page 55: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/55.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
![Page 56: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/56.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
Both bind that function to the name "square"
![Page 57: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/57.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
Both bind that function to the name "square"
Only the def statement gives the function an intrinsic name
![Page 58: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/58.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
Both bind that function to the name "square"
Only the def statement gives the function an intrinsic name
![Page 59: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/59.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
Both bind that function to the name "square"
Only the def statement gives the function an intrinsic name
![Page 60: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/60.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
Both bind that function to the name "square"
Only the def statement gives the function an intrinsic name
![Page 61: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/61.jpg)
Lambda vs. Def Statements
square = lambda x: x * xdef square(x):
return x * xVSBoth create a function with the same arguments & behavior
Both of those functions are associated with the environment in which they are defined
Both bind that function to the name "square"
Only the def statement gives the function an intrinsic name
The Greek letter lambda
![Page 62: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/62.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
![Page 63: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/63.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
f(x) = x2 ‐ 2
![Page 64: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/64.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
f(x) = x2 ‐ 2
![Page 65: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/65.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
f(x) = x2 ‐ 2 A “zero”
![Page 66: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/66.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
f(x) = x2 ‐ 2 A “zero”
x=1.414213562373095
![Page 67: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/67.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
f(x) = x2 ‐ 2 A “zero”
Application: a method for (approximately) computing square roots, using only basic arithmetic.
x=1.414213562373095
![Page 68: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/68.jpg)
Newton’s Method Background
Finds approximations to zeroes of differentiable functions
f(x) = x2 ‐ 2 A “zero”
Application: a method for (approximately) computing square roots, using only basic arithmetic.
The positive zero of f(x) = x2 ‐ a is
x=1.414213562373095
![Page 69: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/69.jpg)
Newton’s Method
Begin with a function f and an initial guess x
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
![Page 70: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/70.jpg)
Newton’s Method
Begin with a function f and an initial guess x
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
![Page 71: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/71.jpg)
Newton’s Method
Begin with a function f and an initial guess x
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
![Page 72: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/72.jpg)
Newton’s Method
Begin with a function f and an initial guess x
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
Compute the derivative of f at the guess: f'(x)
![Page 73: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/73.jpg)
Newton’s Method
Begin with a function f and an initial guess x
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
Compute the derivative of f at the guess: f'(x)
Update guess to be:
![Page 74: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/74.jpg)
Newton’s Method
Begin with a function f and an initial guess x
(x, f(x))
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
Compute the derivative of f at the guess: f'(x)
Update guess to be:
![Page 75: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/75.jpg)
Newton’s Method
Begin with a function f and an initial guess x
(x, f(x))
‐f(x)
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
Compute the derivative of f at the guess: f'(x)
Update guess to be:
![Page 76: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/76.jpg)
Newton’s Method
Begin with a function f and an initial guess x
(x, f(x))
‐f(x)/f'(x)
‐f(x)
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
Compute the derivative of f at the guess: f'(x)
Update guess to be:
![Page 77: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/77.jpg)
Newton’s Method
Begin with a function f and an initial guess x
(x, f(x))
‐f(x)/f'(x)
‐f(x)
Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif
Compute the value of f at the guess: f(x)
Compute the derivative of f at the guess: f'(x)
Update guess to be:
![Page 78: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/78.jpg)
Using Newton’s Method
![Page 79: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/79.jpg)
Using Newton’s Method
How to find the square root of 2?
![Page 80: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/80.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
![Page 81: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/81.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
f(x) = x2 ‐ 2
![Page 82: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/82.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
f(x) = x2 ‐ 2
![Page 83: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/83.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
How to find the log base 2 of 1024?
f(x) = x2 ‐ 2
![Page 84: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/84.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
How to find the log base 2 of 1024?>>> g = lambda x: pow(2, x) - 1024>>> find_zero(g)10.0
f(x) = x2 ‐ 2
![Page 85: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/85.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
How to find the log base 2 of 1024?>>> g = lambda x: pow(2, x) - 1024>>> find_zero(g)10.0 g(x) = 2x ‐ 1024
f(x) = x2 ‐ 2
![Page 86: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/86.jpg)
Using Newton’s Method
>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951
How to find the square root of 2?
How to find the log base 2 of 1024?>>> g = lambda x: pow(2, x) - 1024>>> find_zero(g)10.0 g(x) = 2x ‐ 1024
f(x) = x2 ‐ 2
![Page 87: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/87.jpg)
Special Case: Square Roots
![Page 88: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/88.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
![Page 89: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/89.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
![Page 90: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/90.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
![Page 91: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/91.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
x ‐ f(x)/f'(x)
![Page 92: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/92.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Update:
Babylonian Method
x ‐ f(x)/f'(x)
![Page 93: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/93.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
Implementation questions:
Update:
Babylonian Method
x ‐ f(x)/f'(x)
![Page 94: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/94.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
What guess should start the computation?
Implementation questions:
Update:
Babylonian Method
x ‐ f(x)/f'(x)
![Page 95: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/95.jpg)
Special Case: Square Roots
How to compute square_root(a)
Idea: Iteratively refine a guess x about the square root of a
What guess should start the computation?
How do we know when we are finished?
Implementation questions:
Update:
Babylonian Method
x ‐ f(x)/f'(x)
![Page 96: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/96.jpg)
Special Case: Cube Roots
![Page 97: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/97.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
![Page 98: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/98.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
Update:
![Page 99: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/99.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
Update:
![Page 100: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/100.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
Update:
x ‐ f(x)/f'(x)
![Page 101: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/101.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
Implementation questions:
Update:
x ‐ f(x)/f'(x)
![Page 102: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/102.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
What guess should start the computation?
Implementation questions:
Update:
x ‐ f(x)/f'(x)
![Page 103: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/103.jpg)
Special Case: Cube Roots
How to compute cube_root(a)
Idea: Iteratively refine a guess x about the cube root of a
What guess should start the computation?
How do we know when we are finished?
Implementation questions:
Update:
x ‐ f(x)/f'(x)
![Page 104: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/104.jpg)
Iterative Improvement
![Page 105: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/105.jpg)
Iterative Improvement
First, identify common structure.
![Page 106: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/106.jpg)
Iterative Improvement
First, identify common structure.Then define a function that generalizes the procedure.
![Page 107: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/107.jpg)
Iterative Improvement
def iter_improve(update, done, guess=1, max_updates=1000):"""Iteratively improve guess with update until donereturns a true value.
>>> iter_improve(golden_update, golden_test)1.618033988749895"""k = 0while not done(guess) and k < max_updates:
guess = update(guess)k = k + 1
return guess
First, identify common structure.Then define a function that generalizes the procedure.
![Page 108: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/108.jpg)
Newton’s Method for nth Roots
![Page 109: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/109.jpg)
Newton’s Method for nth Roots
def nth_root_func_and_derivative(n, a):def root_func(x):
return pow(x, n) - adef derivative(x):
return n * pow(x, n-1)return root_func, derivative
def nth_root_newton(a, n):"""Return the nth root of a.
>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):
return x - root_func(x) / deriv(x)def done(x):
return root_func(x) == 0return iter_improve(update, done)
![Page 110: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/110.jpg)
Newton’s Method for nth Roots
def nth_root_func_and_derivative(n, a):def root_func(x):
return pow(x, n) - adef derivative(x):
return n * pow(x, n-1)return root_func, derivative
def nth_root_newton(a, n):"""Return the nth root of a.
>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):
return x - root_func(x) / deriv(x)def done(x):
return root_func(x) == 0return iter_improve(update, done)
Exact derivative
![Page 111: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/111.jpg)
Newton’s Method for nth Roots
def nth_root_func_and_derivative(n, a):def root_func(x):
return pow(x, n) - adef derivative(x):
return n * pow(x, n-1)return root_func, derivative
def nth_root_newton(a, n):"""Return the nth root of a.
>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):
return x - root_func(x) / deriv(x)def done(x):
return root_func(x) == 0return iter_improve(update, done)
x – f(x)/f’(x)
Exact derivative
![Page 112: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame](https://reader034.vdocument.in/reader034/viewer/2022050402/5f7fe7c529338e5d8c39f958/html5/thumbnails/112.jpg)
Newton’s Method for nth Roots
def nth_root_func_and_derivative(n, a):def root_func(x):
return pow(x, n) - adef derivative(x):
return n * pow(x, n-1)return root_func, derivative
def nth_root_newton(a, n):"""Return the nth root of a.
>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):
return x - root_func(x) / deriv(x)def done(x):
return root_func(x) == 0return iter_improve(update, done)
x – f(x)/f’(x)
Definition of a function zero
Exact derivative