Functionally Working with Recursion

03 Feb 2023

Recursion

Recursion expresses a function executing iteratively (over time but that’s a side affect). A function is recursive because it calls itself in a loop; that’s the point. We optimize to reduce iterations, which saves time & resources. When the loop ends, you have final result. The fewer iterations the better: it’s a matter of resource dependency, money spent, time wasted. Like all programs, a recursive function has conditions which defines it’s behaviour. It can sound like life. It’s always true until it’s not. I’m saying we’re meat machines? I’m afraid so. Gross tbh. If you’re curious about read about side effects & recursion,. Recursion is orthogonal to side effects.

Below is some Ruby code. I opened terminal, ran irb, and declared a variable called n by immediately assigning the value of 1. Then I add -1 to n, add 2 to n, and finally add -3.

irb(main):001:> n = 1
irb(main):002:> n - 1
=> 0
irb(main):003:> n + 2
=> 3
irb(main):004:> n - 3
=> -2

It feels pretty straight-forward. Let’s build on it. I’ve defined a function called countdown(n). The functionality of countdown(n) is:

  • the expression n-1
  • runtime condition if n == 0 , or in Ruby: if n.zero?
  • puts prints to the screen
  • call itself

Consider this:

def countdown(n)
  return if n.zero? # base case
  puts n
  countdown(n-1)    # getting closer to base case 
end     
countdown(5) # => output: 5 4 3 2 1

It’s called countdown because that’s what it does – counts down. The countdown function calls itself until there is an appropriate exit condition. When happens, the loop stops & the result has returned. You might think of it as a snake eating its tail.

When you sort a spreadsheet, you’re using recursion. The process of sorting something recursive. The larger the dataset, the slower the sort. The number of iterations grows with the dataset, which takes more time and more resources to process. Recursion is considered somewhat dangerous, to be used hesitantly.

You’ll often see recursion used in functional programming, especially in Ruby. But, I digress.

A note about time.

Time one of the most challenging areas to work with. Working with time, even with a fantastic API like Ruby (linked above), it’s often painful. I honestly avoid it as much as possible.

Allow me a warning: this is a meditation in gratitude, wrapped in the framework of recursion & lambdas. I had been asking myself how to prolong, or save, friendships while living in Australia. Communication can be hard, and some relationships have to come to an end simply due to the nature of the relationship. This is a side affect of time, and to my thinking – recursion. I’m mot saying I’m saying, happy future. 🙂

This is a post about gratitude & recursion, that started as an email. But I have editorial powers here. I can iterate on it, make it good. Efficiency is the path of least resistance, because of course it is. And good company is nice when becoming comfortable. I’ve made friends in Australia. It all works out. Relax the shoulders, release the tension, breathe deep. Manifest gratitude. We’re all good.

I had to find in peace living 15,000 km from friends & family after we relocated from Colorado to South Australia. It took a while, and it was hard; and, for the record, I love Australia. Acknowledging that I’m happy here was key. When I realised that I needed to say “have a happy future & thanks for everything”, I was ironically drawn again to recursion. I wanted to express it properly, so I wrote a blog. I’m filled with gratitude.

In Adelaide, it’s a 13°C summer’s night. We live in the Central Business District (CBD). I like the balcony, hearing the (surprisingly reasonable) downtown noise. A shocking amount of Rainbow Lorikeet come by. The people-watching is off the charts, of course. We haven’t owned a car in nearly a year. If you had asked me, in Denver, a year ago, if I could get by without a car; I’d say, “absolutely not. It sounds romantic but completely impractical.”

Our door is a 300 meter downtown walk from two huge grocery stores. That’s a precise approximation. And precision is superior to conjecture. In Denver, I had no idea but I’d guess maybe 3 miles. That’s a guess. Data-driven decisions multiply when dataset adopts intelligent design. Everyone wins. I’m thankful for the metric system.

I was tempted to title this write-up “Gratitude & Software”. Or, “I Learned To Say Goodbye”. That’s valid, but I digress. Being alive is an exercise in recursion. We’re here because we’re here. And recursion is amazing. We already know that, but I think the former is on the cusp of mathematical proof. I’m grateful for that, so I’ll only some gratitude of my gratitude with me. You can have the rest. A5 out of 5 stars, A++, doubleplusgood, would recommend. I’m filled with gratitude for my friends & family. I love & miss them but they’re well. I hope the same for you. May the force be with you. Commander MeatMachine, signing off.

“I’m happy. I hope you’re happy, too.”