schemeR: Write R code in a Lisp-like form

Description Usage Arguments Value Note Limitations Examples

View source: R/schemeR.R

Description

schemeR() allows embedding Scheme-like code written in prefix format, with the option of using various Lisp operators and features including macros, into R code. The code in question is still syntactically valid R, but has to be converted from prefix to infix to be readily executable. Passing it to schemeR does the conversion and evaluates the resulting 'traditional' R code, with some control over the environment in which the evaluation is done.

Usage

1
schemeR(expr, pkg = FALSE)

Arguments

expr

An expression, usually a block expression between "{" and "}", intended to be in prefix form.

pkg

If FALSE, call eval() with its defaults, envir=parent.frame() and enclos=baseenv(). If TRUE, use envir=parent.frame() but make an enclos environment containing all objects from the package namespace environment and descending from baseenv(). The motivation for this odd semantics is to allow expr to refer to the many exported functions that are standard in Lisp/Scheme (let, letrec, do, cond, etc.) without having to either use the :: operator (which goes against the spirit of Lisp-like prefix code) or require() the package, as would, for example, be good practice when developing another package.

Value

The result of evaluating infix(expr) in the specified environment.

Note

Because R is a Scheme dialect under the hood, prefix or Lisp-like R is still valid R code and parses with the built-in R parser. All of R's rules about syntactic names (see make.names) still apply, and these rules are stricter than usual among Lisps.

Limitations

Because our prefix syntax is just a preprocessing step (R code expressed with the . function is transformed into its usual infix form before evaluation), R's function-call mechanism is still used exactly as-is. In particular, it's still not possible to do tail call elimination safely for arbitrary functions.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
schemeR::schemeR({
.(let, .(.(x, .(c, 1, 3, 5, 7, 9))),
  .(do, .(.(x, .(c, 1, 3, 5, 7, 9), .(cdr, x)),
          .(s, 0, .(`+`, s, .(car, x))),
          .(foo, 4)),
    .(.(is.nil, x), s)))
}, pkg=TRUE) #=> 25

require(schemeR)
schemeR({
.(define, x, .(sort, .(sample, .(`:`, 1, 10), 5, replace=TRUE)))
.(`for`, i, x, .(print, .(`:`, 1, i)))
})

wwbrannon/schemeR documentation built on May 4, 2019, 12:03 p.m.