SlideShare a Scribd company logo
1 of 30
Download to read offline
Passengers on the Tropical Paradise Railway (TPR)
look forward to seeing dozens of colorful parrots
from the train windows. Because of this, the railway
takes a keen interest in the health of the local parrot
population and decides to take a tally of the number
of parrots in view of each train platform along the
main line.
Each platform is staffed by a TPR who is certainly
capable of counting parrots. Unfortunately, the job is
complicated by the primitive telephone system. Each
platform can call only its immediate neighbors.
How do we get the parrot total at the main line
terminal?
Art Belinda Cory Debbie Evan
7 5 3 10 2
Art Belinda Cory Debbie Evan
Solution 1: keep a running total of parrots as we go
Art Belinda Cory Debbie Evan
7
“7 parrots”
7 5 3 10 2
Art Belinda Cory Debbie Evan
12
“12 parrots”
7 5 3 10 2
Art Belinda Cory Debbie Evan
15
“15 parrots”
7 5 3 10 2
Art Belinda Cory Debbie Evan
25
“25 parrots”
7 5 3 10 2
Art Belinda Cory Debbie Evan
27
7 5 3 10 2
Art Belinda Cory Debbie Evan
7 5 3 10 2
“total 27”
Art Belinda Cory Debbie Evan
7 5 3 10 2
“total 27”
Art Belinda Cory Debbie Evan
7 5 3 10 2
“total 27”
Art Belinda Cory Debbie Evan
7 5 3 10 2
“total 27”
Art Belinda Cory Debbie Evan
7 5 3 10 2
27 Final answer!
What did we do?
1.  Count parrots at platform
2.  Add value to the total given by previous station
3.  Call the next station with the new total
4.  Wait for the next station to return with the
total, then pass that along to the previous
station
Art Belinda Cory Debbie Evan
Solution 2: sum the count from the other end
Art Belinda Cory Debbie Evan
7 5 3 10 2
“what’s the total?”
Art Belinda Cory Debbie Evan
7 5 3 10 2
“what’s the total?”
Art Belinda Cory Debbie Evan
7 5 3 10 2
“what’s the total?”
Art Belinda Cory Debbie Evan
7 5 3 10 2
“what’s the total?”
Art Belinda Cory Debbie Evan
2
7 5 3 10 2
“2 parrots”
Art Belinda Cory Debbie Evan
12
7 5 3 10 2
“12 parrots”
Art Belinda Cory Debbie Evan
15
7 5 3 10 2
“15 parrots”
Art Belinda Cory Debbie Evan
20
7 5 3 10 2
“20 parrots”
Art Belinda Cory Debbie Evan
27
7 5 3 10 2
Art Belinda Cory Debbie Evan
7 5 3 10 2
27 Final answer!
What did we do?
1.  Call next station.
2.  Count the parrots at platform.
3.  Add this count to the total given by the next
station.
4.  Pass resulting sum up to previous station.
Solution 2
1.  Call next station.
2.  Count the parrots at
platform.
3.  Add this count to the
total given by the next
station.
4.  Pass resulting sum up to
previous station.
Solution 1
1.  Count parrots at platform
2.  Add value to the total
given by previous station
3.  Call the next station with
the new total
4.  Wait for the next station
to return with the total,
then pass that along to
the previous station
Solution 2
1.  Call next station.
2.  Count the parrots at
platform.
3.  Add this count to the
total given by the next
station.
4.  Pass resulting sum up to
previous station.
Solution 1
1.  Count parrots at platform
2.  Add value to the total
given by previous station
3. Call the next station
with the new total
4.  Wait for the next station
to return with the total,
then pass that along to
the previous station
Tail
Recursion
Solution 2
1.  Call next station.
2.  Count the parrots at
platform.
3.  Add this count to the
total given by the next
station.
4.  Pass resulting sum up to
previous station.
Solution 1
1.  Count parrots at platform
2.  Add value to the total
given by previous station
3.  Call the next station with
the new total
4.  Wait for the next station
to return with the total,
then pass that along to
the previous station
Head
Recursion
Tail
Recursion
Solution 2
1.  Call next station.
2.  Count the parrots at platform.
3.  Add this count to the total
given by the next station.
4.  Pass resulting sum up to
previous station.
Solution 1
1.  Count parrots at platform
2.  Add value to the total given by
previous station
3.  Call the next station with the
new total
4.  Wait for the next station to
return with the total, then pass
that along to the previous
station
In each approach, who knows the total
number of parrots (the final answer)?
What information is being passed to the
recursive call in each approach?

More Related Content

More from Gail Carmichael

References Are 'Nice' Pointers
References Are 'Nice' PointersReferences Are 'Nice' Pointers
References Are 'Nice' PointersGail Carmichael
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort AlgorithmGail Carmichael
 
Find the Maximum Algorithm
Find the Maximum AlgorithmFind the Maximum Algorithm
Find the Maximum AlgorithmGail Carmichael
 
Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...
Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...
Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...Gail Carmichael
 
Interactive Storytelling in Games: Next Steps
Interactive Storytelling in Games: Next StepsInteractive Storytelling in Games: Next Steps
Interactive Storytelling in Games: Next StepsGail Carmichael
 
Coherent Emergent Stories - GHC13
Coherent Emergent Stories - GHC13Coherent Emergent Stories - GHC13
Coherent Emergent Stories - GHC13Gail Carmichael
 
Interactive Storytelling (CUSEC 2013)
Interactive Storytelling (CUSEC 2013)Interactive Storytelling (CUSEC 2013)
Interactive Storytelling (CUSEC 2013)Gail Carmichael
 
Understanding the Power of Augmented Reality for Learning
Understanding the Power of Augmented Reality for LearningUnderstanding the Power of Augmented Reality for Learning
Understanding the Power of Augmented Reality for LearningGail Carmichael
 
Interactive Storytelling (CANCON 2012)
Interactive Storytelling (CANCON 2012)Interactive Storytelling (CANCON 2012)
Interactive Storytelling (CANCON 2012)Gail Carmichael
 
Global Context Descriptors for SURF and MSER Feature Descriptors
Global Context Descriptors for SURF and MSER Feature DescriptorsGlobal Context Descriptors for SURF and MSER Feature Descriptors
Global Context Descriptors for SURF and MSER Feature DescriptorsGail Carmichael
 

More from Gail Carmichael (13)

Linked List Operations
Linked List OperationsLinked List Operations
Linked List Operations
 
References Are 'Nice' Pointers
References Are 'Nice' PointersReferences Are 'Nice' Pointers
References Are 'Nice' Pointers
 
Pointers
PointersPointers
Pointers
 
Function Call Stack
Function Call StackFunction Call Stack
Function Call Stack
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Find the Maximum Algorithm
Find the Maximum AlgorithmFind the Maximum Algorithm
Find the Maximum Algorithm
 
Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...
Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...
Thesis Proposal: Creating Satisfying Game Experiences with Coherent Emergent ...
 
Interactive Storytelling in Games: Next Steps
Interactive Storytelling in Games: Next StepsInteractive Storytelling in Games: Next Steps
Interactive Storytelling in Games: Next Steps
 
Coherent Emergent Stories - GHC13
Coherent Emergent Stories - GHC13Coherent Emergent Stories - GHC13
Coherent Emergent Stories - GHC13
 
Interactive Storytelling (CUSEC 2013)
Interactive Storytelling (CUSEC 2013)Interactive Storytelling (CUSEC 2013)
Interactive Storytelling (CUSEC 2013)
 
Understanding the Power of Augmented Reality for Learning
Understanding the Power of Augmented Reality for LearningUnderstanding the Power of Augmented Reality for Learning
Understanding the Power of Augmented Reality for Learning
 
Interactive Storytelling (CANCON 2012)
Interactive Storytelling (CANCON 2012)Interactive Storytelling (CANCON 2012)
Interactive Storytelling (CANCON 2012)
 
Global Context Descriptors for SURF and MSER Feature Descriptors
Global Context Descriptors for SURF and MSER Feature DescriptorsGlobal Context Descriptors for SURF and MSER Feature Descriptors
Global Context Descriptors for SURF and MSER Feature Descriptors
 

Recently uploaded

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

Head and Tail Recursion

  • 1. Passengers on the Tropical Paradise Railway (TPR) look forward to seeing dozens of colorful parrots from the train windows. Because of this, the railway takes a keen interest in the health of the local parrot population and decides to take a tally of the number of parrots in view of each train platform along the main line. Each platform is staffed by a TPR who is certainly capable of counting parrots. Unfortunately, the job is complicated by the primitive telephone system. Each platform can call only its immediate neighbors. How do we get the parrot total at the main line terminal?
  • 2. Art Belinda Cory Debbie Evan 7 5 3 10 2
  • 3. Art Belinda Cory Debbie Evan Solution 1: keep a running total of parrots as we go
  • 4. Art Belinda Cory Debbie Evan 7 “7 parrots” 7 5 3 10 2
  • 5. Art Belinda Cory Debbie Evan 12 “12 parrots” 7 5 3 10 2
  • 6. Art Belinda Cory Debbie Evan 15 “15 parrots” 7 5 3 10 2
  • 7. Art Belinda Cory Debbie Evan 25 “25 parrots” 7 5 3 10 2
  • 8. Art Belinda Cory Debbie Evan 27 7 5 3 10 2
  • 9. Art Belinda Cory Debbie Evan 7 5 3 10 2 “total 27”
  • 10. Art Belinda Cory Debbie Evan 7 5 3 10 2 “total 27”
  • 11. Art Belinda Cory Debbie Evan 7 5 3 10 2 “total 27”
  • 12. Art Belinda Cory Debbie Evan 7 5 3 10 2 “total 27”
  • 13. Art Belinda Cory Debbie Evan 7 5 3 10 2 27 Final answer!
  • 14. What did we do? 1.  Count parrots at platform 2.  Add value to the total given by previous station 3.  Call the next station with the new total 4.  Wait for the next station to return with the total, then pass that along to the previous station
  • 15. Art Belinda Cory Debbie Evan Solution 2: sum the count from the other end
  • 16. Art Belinda Cory Debbie Evan 7 5 3 10 2 “what’s the total?”
  • 17. Art Belinda Cory Debbie Evan 7 5 3 10 2 “what’s the total?”
  • 18. Art Belinda Cory Debbie Evan 7 5 3 10 2 “what’s the total?”
  • 19. Art Belinda Cory Debbie Evan 7 5 3 10 2 “what’s the total?”
  • 20. Art Belinda Cory Debbie Evan 2 7 5 3 10 2 “2 parrots”
  • 21. Art Belinda Cory Debbie Evan 12 7 5 3 10 2 “12 parrots”
  • 22. Art Belinda Cory Debbie Evan 15 7 5 3 10 2 “15 parrots”
  • 23. Art Belinda Cory Debbie Evan 20 7 5 3 10 2 “20 parrots”
  • 24. Art Belinda Cory Debbie Evan 27 7 5 3 10 2
  • 25. Art Belinda Cory Debbie Evan 7 5 3 10 2 27 Final answer!
  • 26. What did we do? 1.  Call next station. 2.  Count the parrots at platform. 3.  Add this count to the total given by the next station. 4.  Pass resulting sum up to previous station.
  • 27. Solution 2 1.  Call next station. 2.  Count the parrots at platform. 3.  Add this count to the total given by the next station. 4.  Pass resulting sum up to previous station. Solution 1 1.  Count parrots at platform 2.  Add value to the total given by previous station 3.  Call the next station with the new total 4.  Wait for the next station to return with the total, then pass that along to the previous station
  • 28. Solution 2 1.  Call next station. 2.  Count the parrots at platform. 3.  Add this count to the total given by the next station. 4.  Pass resulting sum up to previous station. Solution 1 1.  Count parrots at platform 2.  Add value to the total given by previous station 3. Call the next station with the new total 4.  Wait for the next station to return with the total, then pass that along to the previous station Tail Recursion
  • 29. Solution 2 1.  Call next station. 2.  Count the parrots at platform. 3.  Add this count to the total given by the next station. 4.  Pass resulting sum up to previous station. Solution 1 1.  Count parrots at platform 2.  Add value to the total given by previous station 3.  Call the next station with the new total 4.  Wait for the next station to return with the total, then pass that along to the previous station Head Recursion Tail Recursion
  • 30. Solution 2 1.  Call next station. 2.  Count the parrots at platform. 3.  Add this count to the total given by the next station. 4.  Pass resulting sum up to previous station. Solution 1 1.  Count parrots at platform 2.  Add value to the total given by previous station 3.  Call the next station with the new total 4.  Wait for the next station to return with the total, then pass that along to the previous station In each approach, who knows the total number of parrots (the final answer)? What information is being passed to the recursive call in each approach?