南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion

Lecture 6 Recursion
Lecture 6 - Recursion

Review:Abstraction
Review: Abstraction

Describing Functions def square(x): """Return X X""" A function's domain is the set of all inputs it might possibly take as arguments. x is a number A function's range is the set of output values it might square returns a non- possibly return. negative real number A pure function's behavior is the relationship it creates square returns the square of x between input and output
Describing Functions A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. def square(x): """Return X * X""" x is a number square returns a nonnegative real number square returns the square of x

Functional Abstraction Demo Mechanics Use (functional abstraction) How does Python execute this program square(2)always returns 4 line-by-line(e.g.Python Tutor) ● square(3)always returns 9 What happens when you evaluate a call expression,what goes on its body,etc. Without worrying about how Python evaluates the function
Functional Abstraction Mechanics How does Python execute this program line-by-line (e.g. Python Tutor) What happens when you evaluate a call expression, what goes on its body, etc. Use (functional abstraction) ● square(2) always returns 4 ● square(3) always returns 9 ● ... Without worrying about how Python evaluates the function Demo

Recursion
Recursion

Suppose you're waiting in line for a concert. You can't see the front of the line,but you want to know what your place in line is.Only the first 100 people get free t- shirts! You can't step out of line because you'd lose your spot. What should you do?
Suppose you're waiting in line for a concert. You can't see the front of the line, but you want to know what your place in line is. Only the first 100 people get free tshirts! You can't step out of line because you'd lose your spot. What should you do?

An iterative algorithm might say: 1.Ask my friend to go to the front of the line. 2.Count each person in line one-by-one. 3.Then,tell me the answer
An iterative algorithm might say: 1. Ask my friend to go to the front of the line. 2. Count each person in line one-by-one. 3. Then, tell me the answer

A recursive algorithm might say: If you're at the front,you know you're first Otherwise,ask the person in front of you, "What number in line are you?" The person in front of you figures it out by asking the person in front of them who asks the person in front of them etc... Once they get an answer,they tell you and you add one to that answer
A recursive algorithm might say: • If you're at the front, you know you're first. • Otherwise, ask the person in front of you, "What number in line are you?" • The person in front of you figures it out by asking the person in front of them who asks the person in front of them etc… • Once they get an answer, they tell you and you add one to that answer

Recursion Recursion is useful for solving problems with a naturally repeating structure-they are defined in terms of themselves It requires you to find patterns of smaller problems,and to define the smallest problem possible
Recursion Recursion is useful for solving problems with a naturally repeating structure - they are defined in terms of themselves It requires you to find patterns of smaller problems, and to define the smallest problem possible

Recursion in Evaluation f(g(h(2),True),h(x)) g(h(2),True) h(x) A call expression is composed of smaller (call) expressions! h(2) Stop once you reach a number, boolean,name, etc
Recursion in Evaluation f(g(h(2), True), h(x)) g(h(2), True) h(2) h(x) A call expression is composed of smaller (call) expressions! Stop once you reach a number, boolean, name, etc
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Concurrent Assembly Code with Dynamic Thread Creation and Termination.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Assembly Code with Stack-Based Control Abstractions.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)An Open Framework for Foundational Proof-Carrying Code.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Certifying Low-Level Programs with Hardware Interrupts and Preemptive Threads.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)On the Relationship between Concurrent Separation Logic and Assume-Guarantee Reasoning.ppt
- 《计算机科学》相关教学资源(参考文献)Technical Report TTIC-TR-2008-1(Local Rely-Guarantee Reasoning).pdf
- 《计算机科学》相关教学资源(参考文献)Deny-Guarantee Reasoning.pdf
- 《计算机科学》相关教学资源(参考文献)A Rely-Guarantee-Based Simulation for Verifying Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Modular Verification of Linearizability with Non-Fixed Linearization Points.pdf
- 《计算机科学》相关教学资源(参考文献)Characterizing Progress Properties of Concurrent Objects via Contextual Refinements.pdf
- 《计算机科学》相关教学资源(参考文献)Rely-Guarantee-Based Simulation for Compositional Verification of Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Compositional Verification of Termination-Preserving Refinement of Concurrent Programs.pdf
- 《计算机科学》相关教学资源(参考文献)A Program Logic for Concurrent Objects under Fair Scheduling.pdf
- 《计算机科学》相关教学资源(参考文献)A Practical Verification Framework for Preemptive OS Kernels.pdf
- 《计算机科学》相关教学资源(参考文献)Progress of Concurrent Objects with Partial Methods.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)24-SQL-II.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)25-Conclusion, and Final Exam Review.pptx