[R-SIG-Mac] A shorthand for '<-'
John Chambers
jmc at r-project.org
Wed Nov 21 23:55:57 CET 2007
Duncan Murdoch wrote:
> On 11/21/2007 1:19 PM, Simon Urbanek wrote:
>> On Nov 21, 2007, at 11:45 AM, John Chambers wrote:
>>
>>> Simon Urbanek wrote: James,
>>>
>>> On Nov 21, 2007, at 8:22 AM, James Milks wrote:
>>>
>>>> The '=' sign can be used in place of '<-'. That's the only
>>>> shorthand I know for R.
>>>>
>>>
>>> That's not a shorthand. `=` and `<-` are semantically different in R.
>>> Well, not really :-)
>>>
>>> It's true that the "=" operator won't be displayed as "<-", which I
>>> agree was the original point. But both assignment operators map
>>> into the same internal C code, if you di g into the implementation.
>>>
>>
>> I think we may be talking about different things here.
>>
>> > a=list(a=1,b=2)
>> > ls()
>> [1] "a"
>>
>> > a<-list(a<-1,b<-2)
>> > ls()
>> [1] "a" "b"
>>
>> So `<-` and `=` are *not* semantically equivalent (where `<-` and
>> `=` represent symbols in the parse tree).
Ouch! That's a good example of why people complained when "=" was added
as an operator. In your first expression, "=" is being used in two
quite different ways. (You can make out a case that argument
specification is a little like assignment, since it creates an object of
that name in the environment of the call, but that's pretty subtle.)
In a sense "=" inside a function call is not semantically an operator at
all; it's absorbed by the parser into the internal structure that
represents the call. In contrast, putting an assignment operator inside
an argument to a function is very different (and, IMO, something to be
avoided in writing clear S-language code.)
The difference can be highlighted by looking at the parsed but
unevaluated expressions in the two cases:
> e1 = quote(list(a=1,b=2))
> e1[[2]]
[1] 1
> names(e1)
[1] "" "a" "b"
> e2 = quote(list(a<-1, b<-2))
> e2[[2]]
a <- 1
The "=" has in a sense disappeared, only affecting the names attribute
of the language object.
>>
>> And I suppose the misunderstanding comes from the interpretation of
>> `=` and `<-`: I meant them as symbols (which is what I would expect
>> since we're talking about writing R code) and you interpreted them
>> as operators (which cold be expected given that I used backticks
>> which was not wise ;)). You are right that `=` and `<-` are
>> equivalent as operators:
>>
>> > `=`(a,list(`=`(a,1),`=`(b,2)))
>> > ls()
>> [1] "a" "b"
>>
>> I hope this makes things even more clear ;).
>
> Of course, even using them as operators the parser doesn't think they
> are identical:
>
> > if (a <- 1) cat("yes\n")
> yes
> > if (a = 1) cat("yes\n")
> Error: unexpected '=' in "if (a ="
> > if (`=`(a,1)) cat("yes\n")
> yes
Again, the parser prohibition comes from catching a classic programming
error in C (and another complaint about using "=" as an assignment
operator).
while(flag = 0) { // of course, I meant (flag == 0)
...
if(something(...)) flag = 1;
}
after which the programmer wonders why the loop never exits.
(These points recall some surprisingly religious arguments 10 years or
so ago ;-))
>
> Duncan Murdoch
>
More information about the R-SIG-Mac
mailing list