Discover more from Capsule Reviews
5 of Swords: Recursion
To solve the whole problem, first solve it a little bit, and then solve the rest
The suit of swords, in computing, represents the reflexive and universal nature of the computer. The fifth card in a Tarot suit is the essence of the suit places that suit as an intruder, challenging and disrupting the world. The five of swords represents recursion - the ability of a computer program to invoke itself - that magic trick and cornerstone.
def find(needle, haystack) if haystack.empty? false elsif haystack == needle true else find(needle, haystack[1..]) end end
For recursion to work, it must have a base case. Each recurrence must be for a smaller problem than one before, converging on an answer. If the problem doesn’t get smaller, then you have induction, diverging out into infinity: that works for mathematics, but not for our finite computers.
Consider the definition of the issue you’re encountering. What are the trivial cases or zeroes? What are the next steps up the mountain? How will you know when you’re finished?