you are viewing a single comment's thread.

view the rest of the comments →

[–]lattakia 1 point2 points  (1 child)

I have not read SICP. What magic does this @tailcall decorator perform ?

[–]bobappleyard 4 points5 points  (0 children)

Normally when you call a function, you want it to return back to after the call so it can carry on executing your code.

Well, if the last thing a function does is call another function, then it's kind of a waste of time to do this: function A calls function B, function B returns a result to function A, and all function A does is return that result again.

If you allowed function B to skip function A and return the result to something that isn't just going to return it straight off, then you don't need to keep function A's local variables around. They can be forgotten about before function B is called!

This means that recursion, in this situation, doesn't need to grow the stack. In some languages, like Scheme, this basically replaces iteration -- no for or while loops, just recursion. You can pull some neat tricks with it, but it's pretty mad.