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

2008-04-06 Thread Bill.Venables
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.

There are three left assignment operators in S, and it's a good idea to
distinguish what they do.

a - b
assigns a value 'b' to an object 'a' in some parent environment

a - b
assigns a value 'b' to an object 'a' in the current environment

a = b (inside the argument list of a function call)
potentially assigns a value 'b' to an object 'a' in the child
environment of the function.  Lazy evaluation determines if it actually
happens or not.

You must use '=' for the third of these.  If you choose to use it for
the second as well, as is allowed, there is a danger that you will
confuse the environment in which the assignment is actually made.  It's
not a great danger, of course, but it can happen.  In any case, it is a
good idea to use three separate operators for these three distinct
purposes, if nothing else as a clear visual reminder of what kind of
assignment is intended to take place.

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.

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile: +61 4 8819 4402
Home Phone: +61 7 3286 7700
mailto:[EMAIL PROTECTED]
http://www.cmis.csiro.au/bill.venables/ 

__
R-help@r-project.org 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.


Re: [R] What to use for assignment, = or - ?

2008-04-06 Thread Ben Bolker
 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.)

  Ben Bolker

__
R-help@r-project.org 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.


Re: [R] What to use for assignment, = or - ?

2008-04-06 Thread Charilaos Skiadas
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

__
R-help@r-project.org 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.