Wait A Minute...Is Recursion Ever the Best Solution?


#1

I’m fascinated by recursion – it bugs me even as it tickles me – but in my (limited) reading so far, there doesn’t really seem to be any good arguments for its actual use: it’s resource-intensive and it’s harder to read/follow…so what are the use-cases where it’s the best or even only solution???

Just curious. Thanks!


#2

Recursion will typically be more expensive than iteration however, Recursion allows for simple and elegant solutions to problems. In fact it can be a lot easier to understand/follow a well written recursive function.
A recursive solution aims to break a big problem into one small problem. Things like visiting nodes on a tree are made simple with recursive solutions.


#3

Oh, okay – just looked up trees and nodes – so okay, thanks! No wonder they don’t typically introduce recursion at the beginners’ levels…


#4

It can be a lot to digest!
I wouldn’t worry about it for now.


#5

Well, I dunno, actually…recursion’s all around us in everyday life so it’s not far-fetched at all as a concept…but yeah, to talk about when it’s actually best and just downright necessary, yes, it would be rather much then, at the beginners’ level!

Still, I’m kinda hooked now…'specially as it plays such a central role in any program complex enough to be worth any money to a customer!


#6

Actually, is there a resource that trains how to think recursively??

Not just what it is (https://www.sitepoint.com/recursion-functional-javascript is a good one, BTW), not just examples of it, but how to learn it, to think in it, with, ideally, exercises that build the skill?


#7

Look up Towers of Hanoi recursion


#8

I’ve actually heard of “Towers of Hanoi” but do you mean to suggest that it would train me in recursive thinking and problem-solving??


#9

Yep, There are recursive solutions to the problem.
It will help you see the differences between iterative and recursive solutions as well.


#10

No, wait, I understand the difference between recursion and iteration…what I’m looking for is training in thinking recursively!

No such site/book on how to think recursively?? Just devoted to it (beginner-friendly, of course; I see some like the classic SICP requires learning a whole new language first!)…


#11

(Though I suppose I can just go learn Scheme and learn my recursion that way, as the language is said to almost revolve around recursion!)


#12

The whole point is you’ve probably seen towers of hanoi done iteratively. You are familiar with it solved that way. Now go see how it’s solved recursively

Don’t overcomplicate things by trying to dive to deep into too many subjects at once.


#13

Ah, I see – thanks…I’ve actually never seen Towers of Hanoi solved but have heard of it and understand it’s a classic algo problem.

As for over-complicating things…well, this is what my screen looks like right now:

See all those open tabs? And the two browsers and Acrobat?? :wink:

Those are all my “research projects” right now…and I’ve actually just cleared off a few to-do items, believe it or not! Lucky I’ve been suspended from work! :smiley:

Honestly, I can’t help but wonder…I don’t like leaving things alone hoping I’ll just somehow magically get it later on (though yes, that does happen too, oftentimes). See, it’s actually 'cause I’m truly quite fascinated by programming and computer science in general that I want to check it all out – right now now now!!! :slight_smile:

==>> Thanks very much for all your advice, though! <<==