A few days ago, I was attending a conference in Batumi, Georgia, where I spoke with Furio Honsell. Furio is one of the smartest people I’ve met, and I was lucky enough to have him supervise my bachelor’s thesis.
He often asks me about Georgian culture. Since he is a mathematician, our conversations tend to drift toward mathematics as well. I enjoy looking for small and surprising details in Georgian culture or language that I can share with him.
That evening, while I was trying to think of another example, my girlfriend reminded me of The Flea and the Ant, a traditional Georgian folk tale called „რწყილი და ჭიანჭველა“ in Georgian. It is a short story, so here is a quick summary.
If you are a programmer, you may already see where I am going with this.
The chain looks a lot like a program’s call stack. Each character has a task they cannot finish yet, so they ask someone else for help and wait.
The same thing happens when one function calls another. The current function pauses, and the new call adds a frame to the stack. This continues until the final function has something to return. The stack then unwinds in reverse order. Each function finishes its work and hands control back to the function that called it.
Interactive walkthrough
Following the call stack
Use the buttons, swipe, or press the left and right arrow keys.
Our Story running Our Story waiting antFalls() running Our Story waiting antFalls() waiting rescueAnt() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() waiting moveMouse() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() waiting moveMouse() waiting getMilk() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() waiting moveMouse() waiting getMilk() waiting getGrass() running Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() waiting moveMouse() waiting getMilk() receives: grass getGrass() returns: grass Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() waiting moveMouse() receives: milk getMilk() returns: milk Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() waiting getMillet() receives: mouse moved moveMouse() returns: mouse moved Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() waiting getChick() receives: millet getMillet() returns: millet Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() waiting moveCrow() receives: chick getChick() returns: chick Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() waiting getAcorn() receives: crow moved moveCrow() returns: crow moved Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() waiting getBristle() receives: acorn getAcorn() returns: acorn Our Story waiting antFalls() waiting rescueAnt() waiting makeRope() receives: bristle getBristle() returns: bristle Our Story waiting antFalls() waiting rescueAnt() receives: rope makeRope() returns: rope Our Story waiting antFalls() receives: ant rescued rescueAnt() returns: ant rescued Our Story receives: journey continues antFalls() returns: journey continues Our Story finished: journey continues Step 1 of 24
Why the order matters
The flea asks the pig for help near the beginning, but the pig cannot help until almost everyone else has done their part. The cow receives the last request, yet gives the first useful result. From there, each result travels backward through the chain.
That reverse order is the important part. A stack follows the last in, first out rule. The most recently called function finishes first. Its frame is removed, and the function below it continues from where it paused.
For example, getMilk() pauses when it calls getGrass(). Once getGrass() returns the grass, its frame is removed and getMilk() resumes. It gets the milk and returns it to moveMouse(). Much later, getBristle() returns a bristle to makeRope(), which turns it into a rope and passes it back to rescueAnt().
Where the analogy fits
The story is not a computer program, and I doubt anyone told it with call stacks in mind. Still, the order is the same. Requests build up in one direction and are resolved in the other.
A real stack frame also stores the information a function needs when it resumes, including its local variables and return address. The characters do something loosely similar. Each one remembers what they promised to do while waiting for the next request to be completed.
Final thoughts
The real lesson of the folk tale is about friendship, persistence, and helping someone who needs you. The call stack is just an accidental second reading, but it made a familiar story feel new to me.
It also gave me a good answer for the next time Furio asks me about Georgian culture. I can tell him that one of our folk tales contains a surprisingly good explanation of the call stack.