Tuesday, September 28, 2004

Breadth is sometimes better than depth

Eric Lippert talks about how you usually use recursion to list all the files in a directory and how to change your code to make this iterative (with an explicit stack).

Recursion just lets you do this algorithm without making the stack explicit; the stack is the call stack and doing the recursive call pushes a new frame on the call stack for you. We can easily do this without recursion by making the stack explicit.

Its written in some language that is called "CoffeeScript" or "Java" or something like that, but if you catch the drift, you should be able to use it in any language. And he continues to show how to change the behaviour from depth-first to breadth-first.


No comments: