What is tail call optimization?

前端 未结 10 1266
一整个雨季
一整个雨季 2020-11-21 06:36

Very simply, what is tail-call optimization?

More specifically, what are some small code snippets where it could be applied, and where not, with an explanation of wh

10条回答
  •  时光说笑
    2020-11-21 07:16

    The recursive function approach has a problem. It builds up a call stack of size O(n), which makes our total memory cost O(n). This makes it vulnerable to a stack overflow error, where the call stack gets too big and runs out of space.

    Tail call optimization (TCO) scheme. Where it can optimize recursive functions to avoid building up a tall call stack and hence saves the memory cost.

    There are many languages who are doing TCO like (JavaScript, Ruby and few C) whereas Python and Java do not do TCO.

    JavaScript language has confirmed using :) http://2ality.com/2015/06/tail-call-optimization.html

提交回复
热议问题