From: Robert Dober Date: 2007-07-20T21:17:47+09:00 Subject: Re: The Pebble in the Ruby Shoe On 7/20/07, Logan Capaldo wrote: > On 7/19/07, M. Edward (Ed) Borasky wrote: > > > > Logan Capaldo wrote: > > > > > You've never written code to traverse some kind of tree? And if you > > have, > > > you did it all iteratively? I would suggest _lots_ of programmers use > > > recursion, even in C. > > > > There's a big difference between an explicit stack with iterative code > > and recursion with an implicit stack. I've done a lot of explicit stack > > coding for walking data structures -- indeed, the last time I did so was > > a recent piece of code that generates the state space of a large finite > > Markov chain. But the only time I've written recursive functions as a > > *preferred* modus operandi has been in Lisp, Scheme and the Lisp-based > > symbolic math package Derive. And there, I've always aimed for tail > > recursion. > > > > > > Interesting. This is wild speculation of course, but I imagine you are > probably atypical as far as always using explicit stacks. (Since I get the > impression from your post history that you are heavily focused on > performance.). I would guess (more speculation ;)) that if someone were to > grab a set of programs that for instance walked an XML DOM (or a similar day > to day tree structure, like a nested directory) that the vast majority of > those programs would use recursion with implicit stacks. Now that I've > contributed that bit of unsubstantiated nonsense, I'll resume eating my > dinner. :) > I remember a funny use case of explicit stack handling now. About 4 years ago I was writing an XML wrapper around the Python tupleparser. I started recursively, of course and very soon got completely lost, I tried to undo the recursion so that I had full control over some details on the call stack (things like cheating by examining the call stack) never got anywhere but I have to admit it was good to have that technique at my disposal. If I recall correctly I would probably have used a recursive approach with yield in Ruby... But I agree these are rare exceptions and no reason at all not to use recursion, a better design might have allowed for a recursive solution too :) Cheers Robert -- I always knew that one day Smalltalk would replace Java. I just didn't know it would be called Ruby -- Kent Beck