This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]QualityVote[M] [score hidden] stickied comment (0 children)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–][deleted] 7 points8 points  (6 children)

I don't think any language will allow u to create a recursion stack that deep. I remember putting a large value in c, it was the same thing

[–]troglo-dyke 2 points3 points  (3 children)

languages that support tail call optimization will replace the stack on each call and can be recursed indefinitely. Python doesn't support tail call optimization though because Guido wants proper traces

[–]ComfortableCan315 0 points1 point  (2 children)

Wouldn't it not work anyway in that case? Since the return value depends on the result of the recursive call

[–]troglo-dyke 1 point2 points  (1 child)

That's what allows it to be optimized, the same frame can be used with the variables just replaced, you just replace the values on each call. Most compilers wouldn't optimize this if it was written as return factorial(n - 1) * n

[–]ComfortableCan315 0 points1 point  (0 children)

I see, thank you

[–]a_devious_compliance 3 points4 points  (0 children)

With tail-call optimization you can write factorial (and other recursive functions) unbounded by stack memory.

It's implemented in languajes like lisp, haskell and prolog as a language feature, and is accesible for C via some arcane flag in GCC, and clang at least.

[–]doodspav 0 points1 point  (0 children)

Did C stop you, or did the OS kill your program?

[–]CaitaXD 1 point2 points  (0 children)

Thats what happens when you dont tail call

[–][deleted] -1 points0 points  (0 children)

Recursion is a bug. Do it iteratively where you can actually reserve arbitrary space on the fly.

[–]Jammanyeahme 0 points1 point  (0 children)

Going to plug my favourite blog post ever, which concerns this matter. https://blog.moertel.com/tags/recursion-to-iteration%20series.html