[R] Is = now the same as <- in assigning values

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Dec 18 19:37:22 CET 2008


Kenn Konstabel wrote:
> Hi,
>
> On Tue, Dec 16, 2008 at 9:13 AM, Wacek Kusnierczyk <
> Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
>
>   
>> ... but this is also legal if you really hate <- :
>>
>> foo({x = 2})
>> # assign to x, pass to foo as a
>>
>>     
>
> This is legal but doesn't do what you probably expect -- although
> documentation for `<-` says the value (returned by <-) is 'value' i.e.
> whatever is on the right side ...
>   

as far as i can see, this does precisely what i expect -- it assigns 2
to x and then passes x as the argument a to foo.  did you mean there is
something else happening here?

>   
>> x<-NULL  # just to make sure it's not yet 42
>> foo <- function(a) a
>> foo({x = 42})   # no result
>> x
>>     
> [1] 42
>
> If you really hate <-, you should do either
> foo({(x=42)})   # or ....
> foo({x=42; x})  # or even ...
> foo(a=force(x=43))
>   

yes, i think the last one should be the only valid version.  unless you
invent some more complicated syntax...

> As for = being more intuitive, my favorite example is x=x+1 (how on earth
> can x equal x+1 ??? ... wait, this is an assignment ¤#§£$@{{!!!).
>   

who said = is more intuitive for assignments?  i said i prefer it, and
that's because of aesthetics, silly me.  in an earlier post, someone
said it is more natural for his students [1].  argue to the contrary.

it depends on how you program, mostly.  if you're doing functional
programming with no reassignments, = is just perfect.

how on earth can x equal x+1?  there is a solution, guess yourself.  but
in r, the semantics of 'x = x + 1'is *not* that x equals x + 1, so
where's the problem.  in a language where = means comparison (e.g., f#),
the expression evaluates to false, no problem.  in a language where =
means unification (e.g., oz), the expression gives a unification
failure.  but there is a long tradition in programming languages of
using = for assignment -- e.g., in fortran.

but even where = means comparison, x = x + 1 may actually evaluate to
true, just as x = x may evaluate to false.  (have you never seen x == x
evaluate to false in java or c, for example?)

vQ



[1] http://tinyurl.com/4o4ha4



More information about the R-help mailing list