Is it possible to use continuations to make foldRight tail recursive?
The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style. In Scala this would mean that: def foldBack[T,U](l: List[T], acc: U)(f: (T, U) => U): U = { l match { case x :: xs => f(x, foldBack(xs, acc)(f)) case Nil => acc } } can be made tail recursive by doing this: def foldCont[T,U](list: List[T], acc: U)(f: (T, U) => U): U = { @annotation.tailrec def loop(l: List[T], k: (U) => U): U = { l match { case x :: xs => loop(xs, (racc => k(f(x, racc)))) case Nil => k(acc) } } loop(list, u => u) } Unfortunately, I still get a stack overflow for