On 23-Aug-10 17:50:44, Barry Rowlingson wrote: > On Mon, Aug 23, 2010 at 6:06 PM, Davor Cubranic <cubra...@stat.ubc.ca> > wrote: > >> 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. > > Oops yes, negative logic assumption from me. > > Okay, back to the question... > > Looks tricky, because if(x=2){...} fails because of syntax. > There's stuff in gram.y that makes x=1 a bit special, and only > lets it occur as a top-level expression. However x<-1 is allowed > anywhere an expression can be. Both expressions then call the > same 'primitive' function. At that point I'm not sure how the > code could find out if it's being called from a <- or an = > assignment. And then whether its a top-level expression or not. > And this would need checking on every <- call, which could be > a problem... > > Barry
Indeed! R has a number of these tricky little syntatic traps, perhaps especially where precedence or operators is concerned. For example: 1:10-1 # [1] 0 1 2 3 4 5 6 7 8 9 1-1:10 # [1] 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -1:10 # [1] -1 0 1 2 3 4 5 6 7 8 9 10 1+ -1:10 # [1] 0 1 2 3 4 5 6 7 8 9 10 11 1+ -(1:10) # [1] 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 In due course people will learn (most of) the details of precedence, but certainly at the beginning stage I would strongly recommend putting brackets round *anything* which is to be considered as an entity, just to avoid getting things wrong. So, with the item in the original query: if (x<-3) do_something; if they wrote it as if (x<(-3)) do_something; there would be no problem (and no doubt about what went with what). Of course in complicated expressions this could induce an episode of ocular lispopia, but apart from that it's safe! It's certainly something I tend to do even now, simply for the sake of readability (aka "visual parsing"). Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.hard...@manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 23-Aug-10 Time: 20:13:26 ------------------------------ XFMail ------------------------------ ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel