On Thu, Aug 06, 2020 at 04:08:29PM -0700, Christian Seberino wrote: > > Trying to maintain that recursive list of unclosed lists in your > > brain is fun. It stretches the brain in interesting ways. I was > > way into Lisp at one point, including writing several Lisp > > interpreters (that simple structure makes Lisp very easy to > > implement). But I never found Lisp code very maintainable, because > > any time you read a program, you have to build up that list in your > > head every time just to follow the logic structure and figure out > > what the return value will be. > > So basically while recursion is easy to write it isn't so easy for other > people to understand compared to iterative code. So maybe that is a > big part of it....recursion is just harder on the brain than iteration.
Not necessarily. Some problems very naturally lend themselves to recursive solutions. Fibonacci's sequence is one. #!/usr/bin/python def fib(x): if x < 1: raise "arg must be >= 1" if x == 1: return 0 elif x == 2: return 1 else: return fib(x - 1) + fib(x - 2) Pretty straightforward. Now try yourself to write the iterative version. If you haven't done this recently, there's a pretty good chance you'll get it wrong on your first try. The recursive version is a bit simpler and more straightforward to understand, requiring less state to be preserved (and mentally retained, if you're trying to work through the algorithm mentally or on paper). It's also probably significantly slower, so you'd likely still want to use the iterative version, unless you're trying to illustrate how recursion works or care more about writing easily understood code than performance.. -- https://mail.python.org/mailman/listinfo/python-list