Why can't tail calls be optimized in JVM-based Lisps?

后端 未结 3 2022
情书的邮戳
情书的邮戳 2020-12-08 08:15

Main question: I view the most significant application of tail call optimization (TCO) as a translation of a recursive call into a loop (in cases in which the recursive call

3条回答
  •  半阙折子戏
    2020-12-08 08:43

    Real TCO works for arbitrary calls in tail position, not just self calls, so that code like the following does not cause a stack overflow:

    (letfn [(e? [x] (or (zero? x) (o? (dec x))))
            (o? [x] (e? (dec x)))]
      (e? 10))
    

    Clearly you'd need JVM support for this, since programs running on the JVM cannot manipulate the call stack. (Unless you were willing to establish your own calling convention and impose the associated overhead on function calls; Clojure aims to use regular JVM method calls.)

    As for eliminating self calls in tail position, that's a simpler problem which can be solved as long as the entire function body gets compiled to a single JVM method. That is a limiting promise to make, however. Besides, recur is fairly well liked for its explicitness.

提交回复
热议问题