On Wed, Apr 1, 2009 at 10:55 AM, Duncan Murdoch <[email protected]>wrote:
> On 4/1/2009 10:38 AM, Stavros Macrakis wrote:
>
>> As far as I can tell from the documentation, assignment with = is
>> precisely
>> equivalent to assignment with <-. Yet they call different primitives:
>>
>
> The parser does treat them differently:
>
> > if (x <- 2) cat("assigned\n")
> assigned
> > if (x = 2) cat("assigned\n")
> Error: unexpected '=' in "if (x ="
>
Interesting way of handling the classic C glitch (some of us would say
"design flaw in C", but...)
> The ?"=" man page explains this:
>
> " The operator '<-' can be used anywhere,
> whereas the operator '=' is only allowed at the top level (e.g.,
> in the complete expression typed at the command prompt) or as one
> of the subexpressions in a braced list of expressions. "
>
> though the restriction on '=' seems to be described incorrectly:
> > if ((x = 2)) cat("assigned\n")
> assigned
The restriction is incorrect in many other cases as well, e.g. the following
are all assignments: function()a=3; if(...)a=3; while(...)a=3; a=b=3 (two
assignments), and even a*b=3 (parses as assignment, but `*<-` happens not to
be defined in the default environment).
In fact, the only cases I have found where = does *not* mean assignment is
in functional or array argument position (f(a=2) and f[a=2]), the following
contexts with function-like syntax: function(XXX)..., if(XXX)..., and
while(XXX)...; and for (i in XXX).... Are there any others? Perhaps the
documentation could be updated?
As to the difference between the operations of the two primitives: see
> do_set in src/main/eval.c. The facility is there to distinguish between
> them, but it is not used.
So are you saying that it is planned to make = and <- non-synonymous, unlike
a<-b and b->a, which parse the same and are therefore guaranteed to be
synonymous?
-s
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.