How can you solve the right idea for a program recursively?
For some reason, I find it extremely difficult to think recursively and to write recursive programs for university. Using loops, etc., I quickly come up with ideas, and even when I look at the recursive sample solutions, I often wonder how I couldn't have thought of them. Can you reproduce this problem, and do you have any tips?
Recursive solutions are now simply more complex than iterative solutions (i.e. grinding), even if the recursive solutions are usually shorter.
Thinking recursive is, of course, more difficult. Coming to the solution itself also requires unambiguous exercise. This is true for programming anyway. Understanding something on the sheet of paper is the one to apply it later something completely different.
But the truth is also: recursion must be known and can be, but it is not very often used in software development. Of course, this depends entirely on the field of application etc, but nevertheless I can count on a hand how often I have seen or done recursive implementations in the job. In my case, there were also only structures where an object could contain as many objects of the same class. If you have to do something like volume calculation or the like, or if you have to find out whether article x is in the object or any sub-object, you have to work recursively. This is, however, quite self-explanatory.
Rekursion is sometimes not as intuitive as another approach – but it depends partly on what it is about. In recursive structures, recursions are often not only shorter, but also simpler – even where one would otherwise have to maintain a stack.
As always: practice helps. If you think recursive, iterative solutions are more complicated. Finally, you will do something that is important in software development, namely the dismantling of large into small tasks.
Literature tip: https://mitpress.mit.edu/9780262560993/the-little-schemer/
This is quite normal and applies to everything.
It is always harder to find a solution itself than to understand a finished solution. This makes software development so difficult – among others.
And at the recursion, it is also important that recursive branches be kept in mind. If you have a recursive structure (e.g. a tree structure), this is easier because you can imagine better, but you don’t always have an easily understandable recursive structure.
And since there are not so many real examples of *reasonable* recursion, I guess that the recursive tasks that you have put forward are less sensible, which naturally makes it harder. So, for example, there’s something like counting with recursion – then it’s hard to do.
In fact, I usually try to avoid recursion, for some reasons:
Of course, this is always dependent on the situation, for example, recursion in the tree structure can be easier to understand, then I also prefer to recursion.
With time, however, everything becomes easier, so experience in the profession is so important.
Thank you for the helpful answer!
As it says, “In order to understand recursion, you have to understand recursion.” (This sentence is an example of recursion.)
Probably you also have difficulties with complete induction in mathematics – this is the reversal of recursion.
With complete induction, for example, one starts with the statement for the number 0 and then proceeds from a natural number to the next.
In recursion, a similar problem (but a somewhat simpler one) is first solved until one encounters a “triviale” problem.
Perhaps it becomes more understandable with an example:
If you synthesize the natural numbers, you first define a “success relationship” (it is a function):
0 is not successor to a natural number
Each further number has exactly one successor. We call the successor of n’.
(we call 0′ “1”, 1′ “2” etc.)
We define the addition (“m + n”) inductively:
m + 0 := m
m + n’ = m’ + n
If we want to calculate sums, we proceed in reverse direction (“recursive”). B.
5 + 3
3 = 2′
5 + 3 = 5 + 2′ = 5′ + 2
5′ = 6
5 + 3 = 5 + 2′ = 5′ + 2 = 6 + 2
(first recursion step)
6 + 2 = 6 + 1′ = 6′ + 1 = 7 + 1 (second recursion step)
7 + 1 = 7 + 0′ = 7′ + 0 = 8 + 0 (third recursion step)
We arrived at the end of the recursion: 8 + 0 = 8