Skip to content

Handle function definitions inside a function we transform. #22

@mailund

Description

@mailund

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions