[R] If statement generates two outputs
Romain Francois
romain.francois at dbmail.com
Tue Mar 24 10:01:52 CET 2009
Wacek Kusnierczyk wrote:
> Berwin A Turlach wrote:
>
>> G'day Carl,
>>
>> On Mon, 23 Mar 2009 20:11:19 -0400
>> Carl Witthoft <carl at witthoft.com> wrote:
>>
>>
>>
>>
>>> But seriously: can someone explain to me what's going on in the
>>> rvalues.r code? I tried a simple experiment, replacing ":=" with a
>>> "colec" in the code, and of course the line
>>>
>>> c(df1, df2) colec list(4:8, 9:13)
>>>
>>>
>>> just gives me a "syntax error" response. Clearly I need a pointer
>>> to some documentation about how the colon and equals sign get
>>> "special treatment somewhere inside R.
>>>
>>>
>> Not sure why := gets a special treatment,
>>
>
> yet another bug??
>
This is probably due to that in the gram.y file :
case ':':
if (nextchar(':')) {
if (nextchar(':')) {
yylval = install(":::");
return NS_GET_INT;
}
else {
yylval = install("::");
return NS_GET;
}
}
if (nextchar('=')) {
yylval = install(":=");
return LEFT_ASSIGN;
}
yylval = install(":");
return ':';
which gives a meaning to ":=", so that parsing x := 2 makes sense.
> parse( text = "x := 2" )
expression(x := 2)
attr(,"srcfile")
<text>
Romain
>
>> perhaps because it is not a
>> valid name and, hence, the parser deduces that it is an operator?
>>
>>
>>
>
> well, you can't do it with, e.g., ':==', because you'd get a *syntactic*
> error (while ':=' gives a semantic error):
>
> a :== 1
> # syntactic error: unexpected '=' in ':='
>
> ':==' = function(a, b) NULL
> a :== 1
> # syntactic error again, of course
>
> it's indeed surprising that it works with ':=' but not with, e.g., ':>'.
>
> and in cases like ':-' you'd in fact use two operators (so an
> 'overloading' won't work for these):
>
> ':-' = function(a, b) a - if(a > b) b else 0
> 2 :- 1
> # 2 1 0 -1
> # not 1
>
> it's interesting to note that
>
> a :< b
> # error: unexpected '<' in ':<'
>
> will tell you what's unexpected, while
>
> a :% b
> # error: unexpected input in 'a :% b'
>
> a :_ b
> # error: unexpected input in 'a :_'
>
> will leave you wondering what's wrong there.
>
>
>
> vQ
>
> ______________________________________________
> R-help at 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.
>
>
>
--
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
More information about the R-help
mailing list