Skip to content

Instantly share code, notes, and snippets.

@2grep
Created April 10, 2013 05:19
Show Gist options
  • Save 2grep/5352015 to your computer and use it in GitHub Desktop.
Save 2grep/5352015 to your computer and use it in GitHub Desktop.
Newb python question, maybe a general programming question?
I have been struggling to understand programming since I was, oh, 9, and couldn't get beyond drawing a spaceman helmet in Logo (using an octogon and a square). Part of the problem is perhaps that I have received essentially nil formal education on this.
I have been thinking of functions as a recipe. In my world, the program is a recipe box and some of the recipes (functions) call for other recipes. The processor starts executing the master recipe by writing the instructions into RAM and working through them. Like, breakfast crepes. You call the breakfast crepes recipe from Julia Childs. You have to do make the crepe batter once. Then, while you still have crepe batter, you iteratively make crepes. Concurrently, there are various fruit preparations you can make.
I understand recursion is the idea of writing the function calling itself. But, really, the processor is rewriting the function, nested over and over. And perhaps building trees as the copies of the function get nested further and various tests are met.
Well, I just ran the python wiki solution to Project Euler problem 2 (sum of even fibinocci numbers less than 4 million) through pythontutor.com. And I think something occured to me. It seems like every time you conjure a recipe, you don't just use the same processor, you get a gnome with some pots to work on that function. The pots are variables, the gnome works out his recipe, and, if the calling function was expecting return values, the gnome shows the contents of those pots to the caller. The caller may then go back, figure out some more things, and show return values to his caller.
So lets say Al calls Bob to make crepes. Bob makes the batter and calls Charlie to cook them. Charlie cooks a crepe, serves that crepe to Bob, Bob gives it to Al, and goes back to Charlie. Who still exists! Al is unaware that Bob has Charlie stashed in the kitchen, but even after Charlie makes that first crepe, he's still in the kitchen.
But that doesn't really jive with how I understand recursion. Can someone help clear this up for me?
Comments on Hacker News
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment