DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

The Tale of ‘Tail Recursion’

Recursive problem solving doesn't scale well. Fortunately, you can get your compiler to transform your into an iterative process to solve the problem.

Mahesh Chand Kandpal user avatar by
Mahesh Chand Kandpal
·
Apr. 30, 17 · Tutorial
Like (14)
Save
Tweet
Share
18.76K Views

Join the DZone community and get the full member experience.

Join For Free

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration).

Recursions are really cool and highly expressive. For example, consider this factorial function:

imageedit_2_7440821029

But if I increase the size of input, the code will blow up.

imageedit_4_3062358181

Recursions don’t scale very well for large input sizes. And in practical way, I want to use recursions when the problem size is complex and big. So, it kind of defeats the purpose. You can't use it when you need it the most.

This is where a fairly interesting set of techniques come in.

Distinction between a procedure and process.

Procedure: The code we write.

Process: The code that runs.

So essentially, the idea is to write a piece of code that gets transformed into an optimized one when we run it.

We can write such code in the following ways:

  • We can write code in an iterative procedure and run it as an iterative process. But it is not expressive.

  • And we can write code in a recursive procedure and run it as a recursive process. Unfortunately, it does not scale. When our input size gets big, it fails.

  • Another technique is to use some compiler magic if we can to write code as a recursive procedure and transform it into an iterative process under the covers. An implementation with this property is called tail-recursive.

imageedit_16_7482961416

So, now it is working and the stack didn’t blow up this time. In order to see how it works, let's analyze the stack trace of both functions:

imageedit_10_4364322468

In a normal recursive function, we are five levels deep. Now let’s see the stack trace of tail-recursive code.

imageedit_12_5615457333

Notice the difference in output. The point here is that there is only one level of stack. Structurally, we didn’t reduce number of recursive calls. We are still going through five recursions. The reason we have only one level of stack is that the compiler was able to optimize it. Very quietly under the covers, the compiler modified this recursion into a simple iteration.

I hope this has been helpful!

Published at DZone with permission of Mahesh Chand Kandpal, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A First Look at Neon
  • How Chat GPT-3 Changed the Life of Young DevOps Engineers
  • Documentation 101: How to Properly Document Your Cloud Infrastructure Project
  • 10 Most Popular Frameworks for Building RESTful APIs

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: