Skip to main content

Posts

Featured

Joy of Recursion

Recursion is such a wonderful way to express the solution to a problem, yet it seems many people still struggle with it.

Yet recursive solutions don't have to be hard to write than non-recursive.  It's all a matter of getting the right perspective on the problem.

A recursive solution always  has two parts. A set of terminating condition and a recursive part.

You solve the problem in the first part and you let recursion do its magic in the second part.

Let me illustrate.

Suppose you want to put commas in large positive integers (for simplicity).  After all, it helps with readability.

Let's start with some examples:

commafy 1  becomes 1
commafy 10 becomes 10
commafy 899 becomes 899
commafy 1899 becomes 1,899
commafy 7891234567 becomes 7,891,234,567

You can see the pattern.

Let's express the solution in words.

if a number has less than 3 digits, just use those.
if a number has more than 3 digits, commafy all but the right most 3 digits, append a comma, and append the righ…

Latest Posts

Joy of TakeCommand/TCC - simple path display

Joy of TCC and TakeCommand - Thinking the TakeCommand way.

Comment your data

Microservice architectures and the IBM Mainframe - Recollections of similarity

Hardcore History by Dan Carlin

Why Fossil-SCM is an excellent choice for introductory programming courses

My favorite feature of REXX and the one I miss most in other languages

Joy of TCC and Take Command - Directory and Command history

Joy of TCC and Take Command - Repeated Dots

Learning regular expressions from Damian Conway