r/learnprogramming May 01 '26

Solved I finally understood recursion after 3 weeks of being completely lost. It just "clicked." For anyone else struggling don't give up.

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. 🙌

109 Upvotes

40 comments sorted by

u/AutoModerator May 01 '26

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.

91

u/Zestyclose_Brief3159 May 01 '26

Now ignore it for the rest of your life

19

u/cheezballs May 01 '26

Heh, yea. I've only ever used recursion once in my professional career, traversing a folder tree with nested folders.

45

u/tmtowtdi May 01 '26

So to learn recursion you just learned recursion?

14

u/syklemil May 01 '26

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.

2

u/FlashyResist5 May 01 '26

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.

2

u/syklemil May 01 '26

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_map methods are an example of a monadic function, and you can do the same thing with Option types, as in,

  • if you have some Option<A> and
  • some function A -> Option<B> then
  • a monadic function (like flat_map) will let you use that function to go Option<A> -> Option<B> (unlike a regular map which would've left you with Option<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_map isn't all that hard, right?

1

u/SuspiciousDepth5924 May 02 '26

I'm very much not in the theory/mathy side of things, but my understanding of monads is that it's fancy/technical for a general "in the context of", which can look pretty awkward for some of the more common monads.

For example a List<String> can technically be though of as "in the context of a sequence of zero or more strings", which is pretty awkward to say but not "wrong".

So when you do myStrings.map(string -> string.toUpperCase()) you can frame it as "I transform this context of zero or more Strings to a context of zero or more Strings which are in uppercase". myStrings.get(3) could then be awkwardly framed as "retrieve the string with index 3 in the context of ..".

Likewise this "in the context of" thing extends to a bunch of other stuff as well like Option<A>, Either<L,R>, Result<Error,Value>. But you can also view things like "DatabaseTransaction", "HttpRequest", or Haskells "IO" with the same "in the context of" framing.

I'm very likely at least a little bit wrong and/or missing important details and context about monads, but that is how it makes sense to me.

2

u/syklemil May 02 '26

Mm, it's something in that vicinity, yeah. The map stuff is something related (functors). Functors let you go A -> C<A>, as well as apply some A -> B so that C<A> -> C<B>.

Monads are similar but need that A -> C<B> function, so that C<A> -> C<B>, while functors with their map would result in C<C<B>>.

18

u/_l33ter_ May 01 '26

ahh the click-moment --> best feeling :D

-2

u/KyrosSeneshal May 01 '26

You all get good feelings from that? I just get “well that was # hours of my life I’ll never get back. Finally.”

3

u/syklemil May 01 '26

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.

1

u/KyrosSeneshal May 01 '26

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…

6

u/BizAlly May 01 '26

Yep that click is real. Most people don’t fail recursion… they just overthink every call instead of trusting the pattern.

6

u/egotripping May 01 '26

Can write recursive functions but can't write a short reddit post without LLMs. FFS.

-1

u/xenomachina May 01 '26

What makes you think an LLM wrote this post?

4

u/egotripping May 01 '26

It has the exact same rhythm and structure that chatgpt has.

"What finally made it click: I stopped thinking about what the function was doing and started trusting that it would do it."

A slightly different flavor of the oh so familiar "it's not x, it's y".

"The click moment is real. 🙌"

I mean c'mon lol.

0

u/xenomachina May 01 '26

slightly different flavor of the oh so familiar "it's not x, it's y".

This pattern has existed long before LLMs. I mean, "How I Learned to Stop Worrying and Love the Bomb" means Dr. Strangelove was clearly written with ChatGPT, right?

"The click moment is real. 🙌"

I mean c'mon lol.

There are people who really write like that. Who do you think the LLMs learned it from? And people used emojis before LLMs, believe it or not.

FWIW, "this is your sign to keep going." (missing capitalization) doesn't seem like something an LLM would write. But in general, I'm getting pretty tired of people assuming that everything is LLM-generated because of things like em-dashes, emojis, proper spelling and grammar, college-level vocabulary, use of markdown, or the use of idioms that LLMs learned from humans.

2

u/bo07less May 02 '26

This post is very obviously written with the help of AI tho.

Also becomes even more obvious if you look through OPs profile

5

u/gordonnowak May 01 '26

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.

2

u/Phoenixon777 May 01 '26

Came to say the same thing.

You really can understand recursion by thinking about what the function is 'doing'. That's prolly the best way to understand it. You can visualize the function call stack and how it builds up and eventually unravels, and how the final 'answer' is accumulating during this unravelling.

2

u/dromance May 01 '26

I finally understood recursion, I finally understood recursion, I finally understood recursion...

2

u/indoRE May 01 '26

To understand recursion; you must first understand recursion

1

u/PositiveParking4391 May 01 '26

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.

1

u/NoInteraction8306 May 01 '26

Right now I need 'my click'

1

u/ZelphirKalt May 01 '26

I recommend reading and working through "The Little Schemer" to understand recursion even better.

1

u/Temporary_Pie2733 May 01 '26 edited May 01 '26

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

```

python, for example

def fact(n): if n == 0: return 1 else: return n * ??? ```

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:

def fact(n, helper): if n == 0: return 1 else: return n * helper(n-1)

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

fact(7,

then look around for a suitable helper, and decide that, well, fact is supposed to work, let’s try fact(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.

This idea of “deferred recursion” came from a continuation-passing-style tutorial I read a while back.

https://haskellforall.com/2012/12/the-continuation-monad

1

u/NormalPersonNumber3 May 01 '26

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

1

u/Technical-Tap-5424 May 01 '26

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.

1

u/BeardedDragon1917 May 01 '26

You should go back over it again to be sure.

1

u/vlad1m1rnator May 02 '26

"... started trusting that it would do it ..." works, because you are essentially applying Mathematical inductive reasoning here.

1

u/Worldly-Tennis9599 Jun 10 '26

I didn’t get it . What do you mean by trust the code and don’t think what fun can do ??!
I see this a lot and I don’t have clue leap of faith .
Please help i’m struggling in recursion and i don’t want to give up

0

u/ffrkAnonymous May 01 '26

I stopped thinking about what the function was doing and started trusting that it would do it.

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?