Beauty of Recursion? Well, there is what I call Mathematical Beauty, and then there is reality.
It is fantastic to prove neat theorems that something is possible by methods like mathematical induction that in some sense use recursion as in if something is true for some base value and it can be shown that if it is true for N then it also is true for N+1, then by a sort of recursion, it is true for every larger value! And, yes, you can write some seemingly neat and compact routines using recursion, but there are many run-time costs as Chris and others can suggest. Where is recursion most useful? I can think of many places but will briefly mention a few. There is a recursive sort algorithm that keeps dividing the data it is handled in two and calls itself recursively to process the first half and then again on the second half and simply merges the sorted results and returns that. Clearly this kind of algorithm calls itself to a depth of about log to the base two times. And a possible advantage here is that this can take some advantage of parallel architectures and some simultaneity. Another general set of recursive applications is anything that wanders tree-structures and looks for items or places new items in an appropriate place. If it is a binary tree, there is a slight similarity with my first example as there is a do_left and a do_right type of recursion but not really. And of course trees can have more than two branches and you can use recursion on other complex structures. But in some cases, you can come up with a non-recursive way to do things by keeping track of where you are and where you have been. Recursion is a good thing to teach but also a horrible thing when misused. A book I read decades ago, called The Little Lisper, showed algorithms such as asking for greater(A, B) to be done sort of like by recursively calling greater(subtract1(A), subtract1(B)) as long as neither A nor B becomes zero. Neat algorithm and tolerable for greater(3,5) returning 5 after a few recursions when 3 has been lowered to zero. Not so good with negative numbers. But greater(10000000000, 10000000001) might take a while and your machine may run out of memory and since you want to return the original value, tail recursion may not be enough unless you modify things a bit such as using a helper function that also includes the original numbers so it returns the right one or ... Many machines simply restrict you to compare numbers only up to the point where an int or float of some kind support it and then the operation of comparison is mostly just done in a few steps in the hardware no matter what size the two things are. Python with support for unlimited size integers though, ... -----Original Message----- From: Python-list <python-list-bounces+avigross=verizon....@python.org> On Behalf Of hongy...@gmail.com Sent: Thursday, December 30, 2021 6:27 PM To: python-list@python.org Subject: Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed On Friday, December 31, 2021 at 7:04:24 AM UTC+8, Chris Angelico wrote: > Neither of these wants to be recursive, and writing them recursively > pollutes the function signature with parameters that really exist just > to be local variables. Passing an accumulator down is a terrible way > to demonstrate the beauty of recursion - it instead shows off how you > can shoehorn anything into recursion and make it worse in the process. Then what cases/scenarios can demonstrate the beauty of recursion? HZ -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list