[Rd] precedence (was 'historical NA question')
Simon Urbanek
simon.urbanek at r-project.org
Wed May 7 23:55:14 CEST 2014
On May 7, 2014, at 5:41 PM, Hervé Pagès <hpages at fhcrc.org> wrote:
> On 05/07/2014 02:01 PM, peter dalgaard wrote:
>>
>> On 07 May 2014, at 21:52 , Simon Urbanek <simon.urbanek at r-project.org> wrote:
>>
>>> On May 7, 2014, at 3:37 PM, Hervé Pagès <hpages at fhcrc.org> wrote:
>>>
>>>> No big deal. These things can be tricky:
>>>>
>>>> https://stat.ethz.ch/pipermail/r-devel/2006-January/036022.html
>>>>
>>>> Sorry I couldn't resist ;-)
>>>>
>>>
>>> Yeah, but that's just yet another trip down the rabbit hole - why is -2 parsed as `-`(2) and not a single constant? Is there a way to express a negative constant in R? Hmm…
>>>
>>
>> It's painful, but
>>
>>> bquote(.(-2)^2)
>> -2^2
>>> eval(bquote(.(-2)^2))
>> [1] 4
>>> bquote(.(-2)^2)[[2]]
>> [1] -2
>>> mode(bquote(.(-2)^2)[[2]])
>> [1] "numeric"
>>
>> The difficulty is that the tokenizer, which recognizes language elements before the parser goes to work on the grammatical structure, is unable to distinguish the -2 in "-2 + 2" from the one in "-2 ^ y".
>
> Are you saying that the tokenizer could be made smarter and recognize
> "-2" as a token in "-2 ^ y", just because you didn't put a space between
> "-" and "2"? So in "-2 ^ y" it would be token, but not in "- 2^y".
> An therefore precedence would now depend on whether there is a space
> after the minus or not, or something like that? I'm confused.
>
Nope, precedence would not change at all. "-2" and "- 2" are two different things - in case you didn't realize that spaces are important, try "a<-2" vs "a< -2". So, again, if -2 is a value then there is no precedence issue, since you have only operator in "-2 ^ y" namely "^". In "- 2 ^ y" you have two operators, so precedence matters.
Cheers,
Simon
> H.
>
>> And since constants are generated by the tokenizer, negative ones are not generated. I don't think it is completely out of reach for the parser to recognize the pattern "unary minus a numeric constant" and fold it into a constant of the opposite sign, but I'm not volunteering... (and anyways, it is part of the bigger issue of general constant folding --- I suppose that Luke has a handle on that.)
>>
>>
>>>
>>>> On 05/07/2014 09:16 AM, John Chambers wrote:
>>>>> On 5/7/14, 5:21 AM, Therneau, Terry M., Ph.D. wrote:
>>>>>> Hadley asked about the Blue book; my shelf still has the earlier brown
>>>>>> book
>>>>>> Becker and Chambers, 1984, S: An interactive environment for data
>>>>>> analysis and graphics.
>>>>>
>>>>> Historically interesting, but there was never a guarantee that Version 3
>>>>> of S (the "blue book") was back-compatible with earlier versions. We
>>>>> gave users some help in "getting on the road" to converting, that was
>>>>> all (see Appendix 4 to the blue book).
>>>>>
>>>>> For that one brief moment, we felt free to innovate.
>>>>>
>>>>> John
>>>>>
>>>>>>
>>>>>> The manual page for precedence is
>>>>>>
>>>>>> $ component select
>>>>>> %x special operator
>>>>>> - unary minus
>>>>>> : sequence operator
>>>>>> ^ ** exponentiation
>>>>>> * / mult/div
>>>>>> + - add/sub
>>>>>> < > <= >= == != logical
>>>>>> ! not
>>>>>> & | and/or
>>>>>> <- -> assignment
>>>>>>
>>>>>> Terry Therneau
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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
>>>>
>>>> --
>>>> Hervé Pagès
>>>>
>>>> Program in Computational Biology
>>>> Division of Public Health Sciences
>>>> Fred Hutchinson Cancer Research Center
>>>> 1100 Fairview Ave. N, M1-B514
>>>> P.O. Box 19024
>>>> Seattle, WA 98109-1024
>>>>
>>>> E-mail: hpages at fhcrc.org
>>>> Phone: (206) 667-5791
>>>> Fax: (206) 667-1319
>>>>
>>>> ______________________________________________
>>>> 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
>>
>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: hpages at fhcrc.org
> Phone: (206) 667-5791
> Fax: (206) 667-1319
>
More information about the R-devel
mailing list