assignment: Assignment operators

Description Usage Arguments Details Value Examples

Description

These functions provide three in-place modification operators for sequences (where "sequence" means vector, list or pairlist), and two Lisp/Scheme-friendly aliases for R's built-in assignment operator <-.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
define()

set()

sset()

set.pos(nm, pos, val)

set.car(nm, val)

set.cdr(nm, val)

Arguments

nm

The target symbol, whose value (a sequence, in the sense of a list, vector or pairlist) should be modified as appropriate and the symbol rebound.

pos

The index (into the target sequence) that set.pos should change.

val

The new value to assign to the target component of the target object.

Details

Three in-place modification operators are provided: set.pos, set.car and set.cdr, analogous to functions frequently found in Lisp dialects. set.pos modifies the element at a particular positional index in a sequence, and set.car is a special case of set.pos for pos = 1. set.cdr modifies the subsequence that excludes the first element.

These operators do "in-place" modification in the sense that they modify the binding of the symbol that is the first argument without the need for an explicit assignment statement.

The define and set functions are aliases for the built-in <-, and are traditional names for variable creation and assignment operators in Scheme. sset is also provided as an alias for <<-. Unlike in Scheme, there's no distinction in R between creating a binding and assigning a value (equivalently, it's not possible to create an unassigned variable), so define is equivalent to set here.

Value

The new value.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
f <- 1:10
set.car(f, 4)
f == c(4, 2:10)

f <- 1:10
set.cdr(f, 1:5)
f == c(1, 1:5)

f <- 1:10
set.pos(f, 3, 10)
f == c(1, 2, 10, 4:10)

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