[R] What to use for assignment, " = " or " <- "?

Charilaos Skiadas cskiadas at gmail.com
Sun Apr 6 21:36:43 CEST 2008


On Apr 6, 2008, at 10:35 AM, Ben Bolker wrote:

>  <Bill.Venables <at> csiro.au> writes:
>
>>
>> I've noticed an increasing tendency for people to use '=' rather than
>> the older '<-' symbol.  When '=' became available as an assignment
>> operator in S-PLUS in the late '90s my first reaction was to  
>> switch to
>> it as well.  Brian Ripley warned me that it was not a good idea.  As
>> usual he was right, but it took a couple of pretty serious
>> finger-burning episodes before I came fully around to his view.
>>
>   [snip]
>> I suspect the push towards using '=' instead of '<-' has two main
>> drivers:
>>
>> 1. the world is full of lazy typists
>>
>> 2. right now there seems to be a big influx of Matlab people into  
>> R, and
>> it makes them feel more at home.
>>
>> Neither of these is much of a reason, I reckon.
>>
>
>    It may not be a good reason, but the reason I usually
> teach "=" rather than "<-" to my students is that they are
> usually learning scripting/programming for the very first
> time, and the "=" syntax for assignment (which as I recall
> was called "gozzinta", for "goes into", in the FORTRAN coloring
> book) seems natural to most people (even though it's
> logically quite different).  They are so overwhelmed by
> learning new things that I don't want to add one more.
> (This is admittedly a judgment call.)

I actually teach my students to use <-, for a number of reasons, but  
in some sense for the same reason you choose to teach = instead: I  
feel that "<-" expresses better the act of assigning.

The problem really is that in mathematics we use = to denote both  
assignment and equality, and are forced to use words like "let" and  
"define" to indicate when an assignment is meant, which I suppose is  
not the norm. In other words, which one is meant is based on  
"context". Context is perhaps a reasonable way to define things in  
mathematics, or most other non-programming settings really, but it is  
definitely not a very good way in programming settings, in my  
opinion. This is the problem with = in this case: It has different  
meanings depending on its context. This can lead to subtle errors,  
not easy to track down.

On another level, <- brings up the severity of the act of assignment  
more clearly. You are about to overwrite whatever meaning that symbol  
had in the past, and replace it with a brand new meaning, and this  
can have a number of quite unpredictable side-effects. So it is not  
something that should be done lightly. In my opinion assignments  
should be used sparingly.

Now, you might argue that when students are first learning  
programming, we should make it as simple as possible for them. Though  
I do respect that view, I think it is also important to not teach  
them "bad practices", and overdoing it on the assignments is such a  
bad practice, IMO. It's hard to change what one has learned early on  
and gotten used to. I've spent a lot of my teaching time trying to  
correct such bad habits on the part of my students, especially on the  
importance of making it clear, when writing down an equation, whether  
that equation is something we know, something we are trying to prove,  
or used as a definition for the LHS of it.

That's my story anyway, and I'm sticking to it.

Sorry if I strayed a bit off topic there

>   Ben Bolker

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list