tdsmith / aRrgh

A newcomer's (angry) guide to data types in R
Other
307 stars 14 forks source link

no ambiguity in assignments #11

Closed flying-sheep closed 10 years ago

flying-sheep commented 10 years ago

Some parsing ambiguities are resolved by considering whitespace around operators. See and despair: x<-y (assignment) is parsed differently than x < -y (comparison)!

but that’s not ambiguous. <- is simply a symbol, so if it occurs together, it’s always the assignment operator. (afaik)

Protonk commented 10 years ago

The ambiguity stems from the somewhat leaky convention that whitespace isn't significant. In this case, whitespace is not significant until it is interposed between two characters which comprise a single symbol.

flying-sheep commented 10 years ago

but identifiers are also single symbols separated by whitespace, and are like this in most programming languages that are considered without significant whitespace. so are keywords: else ifelseif

kevinushey commented 10 years ago

The point is, novice R programmers might write something like

x=1;
y=2;
if(y<-x) print(1) else print(2) ## what? y isn't less than negative x, why do we print 1?

It's ambiguous in that there are two possible ways to parse y<-x, but the parser chooses the '<- is a symbol' case (rightly so). A novice R programmer who doesn't realize <- is used for assignment and hates whitespace would run into surprises like this.

flying-sheep commented 10 years ago

You’re right. Could be a bit clearer on the page, but now i got it :)