flow-control: Flow-control operators

Description Usage Arguments Details Value Examples

Description

These functions provide R versions of several Scheme flow-control operators. cond() and case() are conditional forms, do() is an iteration construct, and or() and and() allow for conditional short-circuit evaluation. when and unless execute expressions or not depending on the value of a test expression. See the vignettes for full details and a more in-depth discussion of how to use these operators.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
or(...)

and(...)

when(test, ...)

unless(test, ...)

case(val, ...)

cond(...)

do(bindings, test, ...)

Arguments

...

The infix form of prefix arguments. Note that for and() and or(), each element of ... is just an arbitrary R expression.

test

A test expression. See 'Details'.

val

The value dispatched by case and compared with the first elements of the other clauses passed.

bindings

A list of variable bindings for do. Each element is itself two or three elements long; the first is a symbol, the second an initial value, and the third an expression evaluated to update the variable on each iteration. If no third element is provided, the variable is not updated.

Details

The test expression for do is either one or two elements, and is interpreted as follows: the first elment controls whether the loop continues; the second element, if provided, is the return value of the loop. If not provided, the first element's value is returned.

The test expression for when and unless is a single expression, and is evaluated non-lazily. when executes its body expressions if the test expression is true; unless executes them if the test expression is false or NULL.

Value

Return values are different for different operators.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# the print("foo") clause will never be reached in either expression
schemeR::schemeR({
.(or, 1==2, TRUE, print("foo"))
.(and, 1==1, 3 > 4, print("foo"))
 }, pkg=TRUE)

schemeR::schemeR({
.(case, .(`+`, 1, 1),
  .(3, .(print, "foo")),
  .(2, .(print, "bar")))
 }, pkg=TRUE)
schemeR::schemeR({
.(cond,
  .(.(`==`, .(`+`, 1, 4), 4), .(print, "foo")),
  .(.(`==`, .(`+`, 1, 3), 5), .(print, "bar")),
  .(TRUE, .(print, "baz")))
 }, pkg=TRUE)
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),
    TRUE)) == 25
 }, pkg=TRUE)

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