I finally understood recursion after 3 weeks of being completely lost. It just "clicked." For anyone else struggling don't give up.
Posted by Ok-Neighborhood4327@reddit | learnprogramming | View on Reddit | 29 comments
Three weeks ago I posted here asking why my brain couldn't wrap around recursion. I was ready to quit. Today I wrote a recursive solution to a tree traversal problem on my own, without looking it up.
What finally made it click: I stopped thinking about what the function was doing and started trusting that it would do it. Just define the base case, trust the rest.
If you're stuck on something right now. this is your sign to keep going. The click moment is real. 🙌
egotripping@reddit
Can write recursive functions but can't write a short reddit post without LLMs. FFS.
xenomachina@reddit
What makes you think an LLM wrote this post?
syklemil@reddit
Though do also bear in mind that the real magic might lie in the weeks of struggle, as per the old monad tutorial fallacy post.
The brain is a physical organ. Sometimes the human body needs time to be able to do a new thing, just like how no amount of watching tutorials will let someone go from squatting just the bar to squatting their body weight in a day.
FlashyResist5@reddit
Good read.
Also are monads just some elaborate inside joke? The only thing I know about them are they are supposedly difficult to understand and just like burritos.
syklemil@reddit
Nah, they're a math / informatics concept, so it's more like they exist in any programming language the same way other concepts like recursion do, but it's not that often that programming languages make some system for them.
E.g.
flat_mapmethods are an example of a monadic function, and you can do the same thing with Option types, as in,Option<A>andA -> Option<B>thenflat_map) will let you use that function to goOption<A> -> Option<B>(unlike a regularmapwhich would've left you withOption<Option<B>>)There are some more rules for actually considering something a monad, but that's kind of the general idea.
There is also some funky stuff people can use it for, which is probably where the reputation comes from … because
flat_mapisn't all that hard, right?BeardedDragon1917@reddit
You should go back over it again to be sure.
ffrkAnonymous@reddit
While that works, I don't think that counts as understanding. I can do lots of math without understanding it.
Can you re-write a basic for() loop / while() loop using recursion?
_l33ter_@reddit
ahh the click-moment --> best feeling :D
KyrosSeneshal@reddit
You all get good feelings from that? I just get “well that was # hours of my life I’ll never get back. Finally.”
syklemil@reddit
Depends. If it's something like a concept, then yeah, I thought everyone got the feel-good brain juices from that.
The reaction you're talking about is more something for situations where what we're trying to understand was actually buggy or poorly designed, and especially if what we were trying to do turns out to be impossible because of it.
KyrosSeneshal@reddit
No, if it’s even a concept that I’m trying to learn like what OP posted it still feels that way. Good to know…
Ok-Neighborhood4327@reddit (OP)
Yah
FlashyResist5@reddit
Function countdown (n) If n == 0 print “blastoff” return print n return countdown(n-1)
indoRE@reddit
To understand recursion; you must first understand recursion
dromance@reddit
I finally understood recursion, I finally understood recursion, I finally understood recursion...
Technical-Tap-5424@reddit
took me years to get that eureka moment, i recently used it to flatten a nested xml message we get via kafka !!
" The ability of a function to call itself is called recursion" was the most shittiest explanation given to us in college and wasted years of my life.
Zestyclose_Brief3159@reddit
Now ignore it for the rest of your life
cheezballs@reddit
Heh, yea. I've only ever used recursion once in my professional career, traversing a folder tree with nested folders.
NormalPersonNumber3@reddit
This feeling of things finally clicking is great, yeah. The biggest one for me was when I finally understood dependency injection (This is a more advanced topic). That took me like... A year for it to click for me, haha
Temporary_Pie2733@reddit
That was literally what the professor teaching us recursion in college would say “trust your recursion”. Using factorial as the example, you know n! = n(n-1)!. Translating to code, you end up with
You need something to compute (n-1)! Well, that would be
fact(n-1), if it worked, so trust that it does.I think it’s a little easier to segue into this when you have first-class functions. You can pass the problem on to caller, so to speak. For example:
If you don’t know how to compute (n-1)!, just make the caller provide a function to do it for you. Then the caller, face with finding 7!, might write
then look around for a suitable helper, and decide that, well,
fact(6)should work, let’s tryfact(7, fact), and lo and behold, it does work.But if the caller always uses the “
fact(n, fact)” trick, you may as well hardcode it, turning the nonrecursive definition into the desired recursive definition.ZelphirKalt@reddit
I recommend reading and working through "The Little Schemer" to understand recursion even better.
NoInteraction8306@reddit
Right now I need 'my click'
BizAlly@reddit
Yep that click is real. Most people don’t fail recursion… they just overthink every call instead of trusting the pattern.
nikhilbelide@reddit
The most important that I learnt about recursion is setting when we need to break it.
As long as we ace that rest will keep going.
tmtowtdi@reddit
So to learn recursion you just learned recursion?
Striking_Rate_7390@reddit
he didn't got that logic before thats the point
gordonnowak@reddit
I mean it sounds like you're roughly ignoring it and putting faith in some prescriptive use of it. that doesn't sound like anything clicked, not to be negative.
PositiveParking4391@reddit
that feeling comes up when you know what normal procedural programming or even OOPs can do without recursion against what recursion bring on the table.
AutoModerator@reddit
To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.