Is it possible to use continuations to make foldRight tail recursive?

前端 未结 4 354
臣服心动
臣服心动 2021-01-02 07:34

The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style.

In Scala this would mean that:

4条回答
  •  迷失自我
    2021-01-02 07:59

    The problem is the continuation function (racc => k(f(x, racc))) itself. It should be tailcall optimized for this whole business to work, but isn't.

    Scala cannot make tailcall optimizations for arbitrary tail calls, only for those it can transform into loops (i.e. when the function calls itself, not some other function).

提交回复
热议问题