Re:Is Lisp still useful in today's world? (2011)
by betterunix on Thursday February 21, 2013 @01:13PM
(#5258972)
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.
Re:Is Lisp still useful in today's world? (2011)
by baddox on Thursday February 21, 2013 @01:50PM
(#5259250)
The 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/02...
Re:Is Lisp still useful in today's world? (2011)
by gruseom on Thursday February 21, 2013 @04:35PM
(#5260612)
Functional interfaces to functions with local state in loops
That's how I write Lisp too. It seems to me the nicest way to minimize overall complexity.
Re:Is Lisp still useful in today's world? (2011)
by wglb on Friday February 22, 2013 @10:31AM
(#5264643)
I remember a correspondence between Knuth and Dijkstra about a deep problem involving four (!) stacks in the recursive solution. One or the other of them stepped back and found an iterative solution that was much easier to understand.
However, whether or not your example shows something that is not inferior is likely a matter of opinion, not fact.
Re:Is Lisp still useful in today's world? (2011)
by Falling3 on Thursday February 21, 2013 @01:06PM
(#5258928)
As far as I know (which isn't too much), there's nothing wrong with that. But since monkeyfacebag mentioned the psychology of code writing, there definitely seem to be algorithms that lend themselves to recursion. At the very least, some algorithms are more intuitively grasped as recursive - and the same obviously holds for iterative.
Re:Is Lisp still useful in today's world? (2011)
by pavelludiq on Thursday February 21, 2013 @01:21PM
(#5259030)
yes, usually anything having to do with recursive structures, like threes, is a good fit for recursive algorithms. In fact, the function I wrote had to generate a directory structure out of a template: https://gist.github.com/pvlpenev/4760658 (and i still used loop :)
But keep in mind that some things, although recursively defined, are better computed in other ways, the most obvious example is a Fibonacci sequence, which if I really needed, I would precompute and stick in an array for constant time access or something like that(maybe memoize a recursive function).
For example it is my opinion that this function is in no ways inferior to a recursive equivalent: