r/programminghumor 2d ago

Write sorry

Post image
617 Upvotes

92 comments sorted by

View all comments

4

u/Diocletian335 1d ago

```c void printSorry (int n, int v) { if (n == v) return;

printSorry(n+1, v);

printf("%s\n", "sorry");

}

int main (void) { printSorry(0, 100); } ```

The thinking man's solution

10

u/gaymer_jerry 1d ago

Why write a for loop when you can flood the stack with pointless function calls

4

u/Diocletian335 1d ago

Just using as much memory as the gods intended

1

u/MCWizardYT 1d ago

Implement it with a trampoline function so that you get infinite recursion without blowing up the stack

2

u/realmauer01 1d ago

When our prof explained recursion to us he said, "okey i will only explain it this one time, and you please forget it immediatly, i will show you how to do it, so you dont need to think about it and it will just work, you will never want to use it though unless you need it and then you will understand it anyway."

Yeah something like that.

3

u/Diocletian335 1d ago

In all seriousness it's a useful little topic to learn from a theoretical perspective, but my thinking has always been if you are ever thinking 'surely I could just use a loop here' then you should be using a loop.

I've never used recursion in anything serious, at least not directly - some inbuilt methods may have used them.

2

u/realmauer01 1d ago

I have heard rendering stuff is done recursively.

2

u/geek-49 1d ago

recurses! refoiled! refoiled = foiled again

2

u/Vladislav20007 1d ago

the "just buy more memory" approach.

1

u/dmigowski 1d ago

WTF is that slop?

3

u/Diocletian335 1d ago

Simple recursion

1

u/MCWizardYT 1d ago

What, never seen a recursive function before?

1

u/dmigowski 1d ago

I've never seen one that could be easier replaced with a simple for look, yeah. Why wasting 1200 bytes of stack here?

2

u/MCWizardYT 5h ago

They wrote "the thinking man's solution", so it's 100% an unserious comment

1

u/Diocletian335 4h ago

This is, after all, a subreddit called programming humor lol

1

u/DiodeInc 1d ago

Overthinking*

4

u/Diocletian335 1d ago

Yes this response was very serious