Você está na página 1de 1

I remember my college professor used to use the Russian doll as an example of recursion.

Russian dolls usually looks like just a wooden doll on the outside, But once you open up the
outer layer there will be another doll that looks similar to the one on the outside, but a little
smaller. Once you open up that smaller doll, there will be another even smaller one inside. This
process will repeat until you get to the smallest doll on the inside. Depending on the size of doll
you have, there could be five to seven layers dolls you can crack open to get to the smallest one.
Some of the key features that make it recursive – It’s a repetitive process – you will have to
crack open each doll in a similar way in order to get into the smallest one. And each layers of
doll will be smaller than the previous, so kind of like our example with calculating factorial, if
we have factorial(6), we would split it down to 6 times factorial(5), then we split up factorial(5)
to 5 times factorial(4), and we make it smaller and smaller until we reach factorial(1) – which is
the smallest/base case of the factorial function. So we go from big to small. Also, it has a “base
case/stopping case” when we reach the smallest one in the center. This will be the case where we
can’t open it up anymore, and is kind of like the trivial stopping case that stops the process.

Another good example of recursion would be computer file system. I myself is usually very
organized when it comes to saving files on my computer. And I would make folders inside
folders so I would be clear which document belongs to which folder. Especially with photo
albums on my computer, I would have a folder that could be named as photo, then inside photo
folder, I would have the years labeled in each sub folders, and in each year sub folder, I would
then have different location/occasions labeled for the different series of photos, and so on. So
when I need to find a photo, I would click through multiple different photo folders repeatedly,
and once I go through different layers of folder entries, I would find the desired photo I want –
which in this case could be thought of as the base case of my recursive process. This is different
from iterative since there is always a simple stopping case, but we have several layers of
recursive calls that we have to go through in order to reach that stopping case.

Human life cycles seem like a really iterative process, and recursion is impossible (unless it’s the
end of the world). Because human will give birth to the child, and the child will grow up and
again give birth to his/her own child, and the same iterative process repeats and goes on for
generations and generation. Unless some disaster happens where the world is destroyed, humans
have lived in this cycle ever since the beginning of mankind. Now this is very different from
recursion, as in recursion, there has to be a base case where we could eventually stop at that
simplest case. In the case of human life cycles, there would never be an instance where human
will all stop giving birth to the next generation, and the cycle ends.

Você também pode gostar