| ozten ( @ 2007-09-03 21:17:00 |
| Current location: | Home |
| Current mood: | busy |
| Entry tags: | 1.2, eval scheme apply learnings, lisp, scheme, sicp |
SICP 1.2 recursion fun
Reading section 1.2 and watching Lecture 1-b. Up until now, I had the false understanding that any procedure which calls itself was considered recursive in terms of both written procedure and runtime process. Aside, that doesn't mean that I shied away from using recursion, and I did have an intuitive feel that some forms of recursion performed better than others.
Why structure a procedure using recursion? Often code written using recursion is easier to understand, for certain classes of problems, like process tree based structures ( XML Nodes ). The book also discusses an algorithm for making change from different coins, where the recursive algorithm is easier to understand.
I had previously missed the distinction between how a procedure is specified in source code, and how the processes runs and evolves through time. SICP refines the definition of recursion specifically for processes into iterative processes and recursive processes. Iterative, doesn't mean that it can't be written using recursion. An operation may call itself, but it does so in constant space in a straight forward manner, but like a for loop changing the state of loop variables.
So how do we frame procedures, so that we get the runtime process we want?
The metaphor of "Bureaucracy" in the video drove the lesson home of explicit state, versus hiding state away in the machinery of the computation. In the bureaucracy lite version, you ask someone to solve a problem. They ask someone else to solve a similar problem and to give the results back to you directly. They have processed one step and are now out of the picture, have passed on the final destination and updated state of the problem. This is an interative process that chips away at the answer in constant space. The procedure may call back into itself, but it is still an iterative processes.
The heavy bureaucracy has you ask someone to solve a problem. They process part of the problem and delegate another part to someone else. When that person is finished they answer back to their manager, instead of passing the finished work to you. They then finish processing the partially solved problem and give you the answer, perhaps claiming credit for all of their child processes work. This hidden state is all of the operations delegating to further operations and awaiting a response to post process and hand back up the chain of command.
I find the domain of SICP ( mathematics ) very challenging, so it takes me a long time to solve the math focused exercises, but I can get the gist of what the problem is trying to get me to think about, and thus make some guesses at the directions for massaging the equations from recursive to iterative and back again.