lexical: Lexical binding constructs

Description Usage Arguments Details Value Note Examples

Description

These functions provide the let, let* and letrec block-scope operators from Scheme. All three of them create temporary variables with specified values, successively evaluate provided body expressions, and return the value of the last expression.

Usage

1
2
3
4
5
let(bindings, ...)

let.star(bindings, ...)

letrec(bindings, ...)

Arguments

bindings

A list whose elements are themselves two element lists: the first element of each is a symbol, the second the value the symbol should be bound to in the environment for evaluating the body statements.

...

Body statements.

Details

All three forms are syntactically identical:

.(LET_OP, .(.(variable, init) ...), expr, expr ...)

where LET_OP is let, let.star or letrec.

They differ in the scope in which the temporary bindings (of names to init expressions' values) exist:

Value

The result of evaluating the final body expression.

Note

R, unlike Scheme, does not have a notion of variable definition separate from assignment. So the init expressions are mandatory here, whereas in Scheme it is possible to create uninitialized variables in the scope of the body block.

The let, let.star and letrec functions take arguments in a way that seems natural in prefix notation, but makes for odd-looking calls in the usual infix form. Calling them "directly" - i.e., other than through prefix code and SchemeR() or infix() - is not recommended.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
schemeR::schemeR({
.(let, .(.(i, 3),
        .(foo, 5)),
   .(`==`, i, .(`-`, foo, 2)))
 }, pkg=TRUE) == TRUE
schemeR::schemeR({
.(let.star, .(.(i, 3), .(foo, 5)),
      .(`==`, i,
              .(`-`, foo, 2)))
 }, pkg=TRUE) == TRUE
schemeR::schemeR({
.(letrec, .(.(i, 3), .(foo, 5)),
     .(`==`, i,
             .(`-`, foo, 2)))
 }, pkg=TRUE) == TRUE

schemeR::schemeR({
.(letrec, .(.(i, 3),
            .(foo, .(lambda, .(n), .(`+`, n, 1)))),
     .(`==`, i, .(foo, 2)))
 }, pkg=TRUE) == TRUE

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