Re: [Rd] Bug in the parser (?) (PR#13504)

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Fri, 06 Feb 2009 11:44:40 +0100

Peter Dalgaard wrote:

> johnc.deva_at_yahoo.com wrote:
>   

>> Full_Name: John C. Deva
>> Version: 2.8.1
>> OS: Fedora Linux 8, 64 bit
>> Submission from: (NULL) (193.200.150.189)
>>
>>
>> I notice that it is possible to redefine 'if' as a function of an arbitrary
>> number of arguments. Such redefined 'if' can then be used as any other user
>> function, except for that the parser still demands exactly three arguments to be
>> given to if. Furthermore, even if 'if' is defined with three arguments, its
>> application must still be made with the original syntax, and not the usual
>> syntax of function application:
>>
>>
>>> `if` <- function(a,b,c)
>>>
>> + {
>> + assign(deparse(substitute(a)), b+c, envir=parent.frame()
>> + }
>>
>>
>>> if (x) 1 else 2
>>> x
>>>
>> [1] 3
>>
>>
>>> if(x, 1, 2)
>>>
>> Error: unexpected ',' in "if(x,"
>>
>> The later expression above should be the only valid when 'if' is redefined as
>> above, but it is not the case.
>>
>
>
> A bug report with a ? in the title is in general ill-advised. If you are
> not sure that something is a bug, then ask a question instead.
>
> In this case, no, it is not a bug. Syntax is syntax and "if(x,1,2)" is a
> syntax error no matter how if() is defined. Parsing is controlled by the
> fact that "if" is a keyword, the function definition is not used at all
> at that stage. `if`(x,1,2) works perfectly well, though, at least until
> you try deparsing it:
>
>   

>> quote(`if`(x,1,2))
>>
> if (x) 1 else 2 > > > Redefining "if" is a really Bad Idea, excepting things like code > analysis tools (which I believe are the main reason it is not explicitly > forbidden). > > i'm not quite sure i'm getting this last point (after that redefining
'if' is a bad idea, which it is, mostly).

on further exploration, i found that r can be used to produce fancy patterns:

'function' = function(...) { print(list(...)); function(...) list(...) } # a function that prints its arguments and calls itself with the arguments and a list of the arguments

# maximize the terminal, minimize the font size, and execute: function(whatever) NULL

enjoy.

vQ



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 06 Feb 2009 - 10:48:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 06 Feb 2009 - 23:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive