-
Notifications
You must be signed in to change notification settings - Fork 2
/
equalAssign.html
91 lines (86 loc) · 3.67 KB
/
equalAssign.html
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<link REL=stylesheet HREF=Rtech.css>
<title>Assignments with the = Operator</title>
</head>
<body>
<h1>Assignments with the <code>=</code> Operator</h1>
The development version of R now allows some assignments to be written
C- or Java-style, using the <code>=</code> operator. This increases
compatibility with S-Plus (as well as with C, Java, and many other
languages).
<p>
All the previously allowed assignment operators (<code><-</code>,
<code>:=</code>, <code>_</code>, and <code><<-</code>)
remain fully in effect. The <code>_</code> is a historical curio and
there seems to be general agreement that it should be phased out.
(The historical origin is as follows. During the early work on S at
Bell Labs, the main time-shared terminal in use was the Execuport, a
heavyweight thermal-paper machine which had, among other oddities, a
key corresponding to the ASCII underscore that printed as a back
arrow. This was adopted as an easy-to-type and natural-looking
assignment operator. The two-character back arrow was the less
convenient, but portable version.)
Where it is allowed, the <code>=</code> operator is semantically
equivalent to all of the earlier assignments, except of course <code><<-</code. However, it is not
allowed in all situations.
<p>
<h3>Limitations Imposed</h3>
<p>
The new assignments are allowed in only two places in the grammar: at
the top level (as a complete program or user-typed expression); and
when isolated from surrounding logical structure, by braces or an
extra pair of parentheses.
<p>
So the following are allowed:
<code><pre>
> y = runif(10)
> zzz = list(a=y, b="Test")
> zzz$a[1] = NA
> for(i in 1:10) { xx = min(rnorm(100)); zzz$a[i] = xx}
</pre></code>
But the following famous C programming error is illegal in the grammar:
<code><pre>
> if(x = 0) 1 else x
Error: syntax error
</pre></code>
Assignments in control structures are generally dangerous; in any
case, to use them you have to either use one of the older assignment
operators, or add an extra level of parentheses.
<p>
There are two reasons for the restricitions:
<ol>
<li> The language already has assignments with <code>=</code>,
known as named arguments in calls. These of course continue to
be interpreted as before. (They are in essence assignments in
the environment created for the call.) Regualar assignments in any
argument in a function call still have to be done with the old
operator, or else surrounded by an extra set of parentheses.<p>
<li> Disallowing the new assignment form in control expressions
avoids programming errors (such as the example above) that are
more likely with the equal operator than with other S assignments.
</ol>
<p>
The restrictions do produce some other limitations that users may find
less intuitive, because some functions are thought of as special. For
example, the <code>quote</code> function.
<code><pre>
> quote(y = 1)
[1] 1
> quote(y = 1:10)
1:10
> quote(y[1] = 1)
Error: syntax error
</pre></code>
The function is implemented as a special that doesn't check argument
names. The first two examples just use <code>y</code> as the
argument name, and the third fails because the expression on the left
of the <code>=</code> operator isn't valid as an argument name.
A future revision of the function could interpret the first two, but the
grammar pretty much rules out the last example.
<hr>
<address><a href="http://cm.bell-labs.com/cm/ms/departments/sia/jmc/">John Chambers</a><a href=mailto:jmc@research.bell-labs.com><jmc@research.bell-labs.com></a></address>
<!-- hhmts start -->
Last modified: Sun Dec 16 17:58:58 EST 2001
<!-- hhmts end -->
</body> </html>