Mark Lewis
1 min readFeb 26, 2022

--

You had professors who told you not to use recursion? IMO, that says very bad things about those professors. There are many problems that benefit greatly from using recursion, mostly problems that can't be solved with simple iteration, where you actually need to try multiple options. Note that many functional languages will optimize the type of recursive functions you have written here and turn them into loops. (See tail recursion for an explanation.)

Here are some fun problems to try if you want things that actually benefit from recursion.

1. Given a BST, find how many values are in a certain range.

2. Given a directed graph, calculate how many distinct paths there are from one node to another.

Write the recursive solution to each of those first, then try writing them without recursion and compare the code complexity. Compare speed as well. My guess is that the recursive solutions are faster for both of those.

--

--

Mark Lewis
Mark Lewis

Written by Mark Lewis

Computer Science Professor, Planetary Rings Simulator, Scala Zealot

No responses yet