Description Usage Arguments Details Value Examples
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
... |
The infix form of prefix arguments. Note that for |
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. |
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.
Return values are different for different operators.
or returns the value of the first expression which when coerced to logical is TRUE, or otherwise returns FALSE;
and returns the value of the first expression which when coerced to logical is FALSE, or otherwise returns TRUE;
case returns the value of the expression associated with its key expression's value, or if the key's value was not in any clause, returns NULL;
cond returns the value of the last expression in the list of them associated with the first clause whose test condition was logically TRUE, or if no condition was TRUE, returns NULL;
do returns the value of the expression provided with its test expression on the first iteration on which the test expression is logically TRUE (if no second expression was provided, the value of the test expression is returned).
when and unless return the value of the last body expression, if the body expressions were evaluated, or NULL.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.