3 ms·
Personally, I find that traversing data structures recursively is more straightforward than iterative approaches, at least for most of the data structures I dea
by betterunix 14y ago
Personally, I find that traversing data structures recursively is more straightforward than iterative approaches, at least for most of the data structures I deal with. This sort of thing, for example:
(defun search (tree val)
(cond
((null tree) nil)
((= (car tree) val) tree)
((< (car tree) val) (search (cadr tree) val))
((> (car tree) val) (search (cddr tree) val))
)
)
I write this sort of code all the time for more complicated structures; iterative solutions would involve keeping track of a bunch of local variables, which only makes the code more difficult to deal with.
- baddox 14y agoThe Little Schemer is the book that got me thinking about data structures recursively (starting with linked lists), and that was later furthered by SICP. http://www.amazon.com/Little-Schemer-Daniel-P-Friedman/dp/0262560992 http://www.amazon.com/Little-Schemer-Daniel-P-Friedman/dp/02... http://mitpress.mit.edu/sicp/full-text/book/book.html http://mitpress.mit.edu/sicp/full-text/book/book.html