From Greg.Snow at imail.org  Tue Aug 24 22:24:00 2010
From: Greg.Snow at imail.org (Greg Snow)
Date: Tue, 24 Aug 2010 14:24:00 -0600
Subject: [Rd] small syntax suggestion
In-Reply-To: <AANLkTim_YLk8rO0CVxtWz35enpGhAa-zDvYHRdq5D-JU@mail.gmail.com>
References: <AANLkTimvja3=JJ-Y8yS=8K+m8ePP7XrCUTEvzg=L9bjn@mail.gmail.com>
	<AANLkTinwC0h+oX2QhcskS_9xRuws=X9XQ2RuGnOxVCyG@mail.gmail.com>
	<04831FEE-4137-484E-8942-427085663D2F@stat.ubc.ca>
	<4C72D9AC.1080906@sciviews.org>
	<AANLkTim_YLk8rO0CVxtWz35enpGhAa-zDvYHRdq5D-JU@mail.gmail.com>
Message-ID: <B37C0A15B8FB3C468B5BC7EBC7DA14CC633B8EB650@LP-EXMBVS10.CO.IHC.COM>

In the mean time you could have student run the following code each time (put it into .Rprofile or something) until they learn good coding practices:

testfunc <- function(expr, value, ok, visible) {
	tmp <- deparse(expr)
	if( grepl( '<- *[0-9.]+ *[])&|]', tmp ) ) {
		warning(' You used "<-" in the above expression,\nit was interpreted as an assignment\nif you wanted a comparison, use a space between the "<" and the "-"')
	}
	TRUE
}

addTaskCallback( testfunc )



you might want to change the warning to cat (and add \n), or extend the regular expression logic to other cases, or ...

But if nothing else getting the warning will reinforce that parens/spaces are a good idea if only to avoid the computer complaining.  The problem is I can think of some false positives that I have not worked out yet how to avoid.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-
> project.org] On Behalf Of ivo welch
> Sent: Tuesday, August 24, 2010 9:03 AM
> To: phgrosjean at sciviews.org
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] small syntax suggestion
> 
> hi ted, philippe, and others---I agree with everything you write about
> good coding practice.    none of us would be writing x<-3, even when
> we want to assign 3.  we know better.   we would at least use a space,
> if not a paren.  alas, my suggestion is not so much for you.  It is
> trying to spare novices that are just beginning to use R some
> unnecessary frustration.   I bet that most of us have made this
> mistake at least once.
> 
> to the extent that it requires a token "<-[0-9]" to recognize this, it
> could be an easy special case/warning/error.  to the extent that it
> requires more, it is probably not worth the hassle.
> 
> [I am a great fan of syntax checking.  I am not a great fan of many
> but the simplest recycling rules (from 1 to N) BY DEFAULT.  It's just
> asking for trouble.]
> 
> regards,
> 
> /iaw
> ----
> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com)
> 
> 
> 
> 
> On Mon, Aug 23, 2010 at 4:27 PM, Philippe Grosjean
> <phgrosjean at sciviews.org> wrote:
> > I tell to my students that it is very important (not only for
> legibility) to
> > place spaces between operands. They have to write such a code like
> this:
> >
> > if (x < -3) do_something
> >
> > That way, there is no ambiguity. Don't you think it's important to
> write
> > clear code, including by using spaces where it makes it easier to
> read,...
> > and less ambiguous, as you just realize?
> >
> > Best,
> >
> > Philippe Grosjean
> >
> > On 23/08/10 19:06, Davor Cubranic wrote:
> >>
> >> On 2010-08-23, at 6:15 AM, Barry Rowlingson wrote:
> >>
> >>> On Sun, Aug 22, 2010 at 4:33 PM, ivo welch<ivo.welch at gmail.com>
> ?wrote:
> >>>>
> >>>> I have found that my students often make the mistake of
> >>>> mixing up comparisons and assignments with negative numbers:
> >>>>
> >>>> ?if (x<-3) do_something;
> >>>
> >>> If you tell your students not to use '<-' for assignment, then they
> >>> can't make this mistake, because = for assignment has additional
> >>> restrictions on it:
> >>
> >> The students are trying to *compare* to a negative number, and trip
> on R's
> >> parsing of "<-". They could use '=' for assignment all they want
> (which I
> >> thought is being discouraged as a code style these days, BTW), and
> they'll
> >> still run into this problem.
> >>
> >> Davor
> >> ______________________________________________
> >> R-devel at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >>
> >
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] small syntax suggestion
Greg Snow