The following blog article shows how in F# foldBack
can be made tail recursive using continuation passing style.
In Scala this would mean that:
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).