For continuation passing style, we write functions inside functions. We cannot handle this right now. Something like this, which is completely valid and working code, and a tail-recursive function, will raise an error.
library(pmatch)
tree := L(v) | T(left : tree, right : tree)
t <- T(L(1), T(L(2), L(3)))
sum_leaves <- function(t, cont = identity) {
cases(
t,
L(v) -> cont(v),
T(left,right) -> {
call_right <- function(left_sum) {
sum_leaves(
right,
function(right_sum) left_sum + right_sum
)
}
sum_leaves(left, call_right)
}
)
}
sum_leaves(t)
sum_leaves_tr <- tailr::loop_transform(sum_leaves)
For continuation passing style, we write functions inside functions. We cannot handle this right now. Something like this, which is completely valid and working code, and a tail-recursive function, will raise an error.