[Rd] anonymous function parsing bug?

peter dalgaard
Fri, 21 Oct 2016 18:13:48 -0000

> On 21 Oct 2016, at 19:17 , Wilm Schumacher <wilm.schumacher at
gmail.com> wrote:
>
> Am 21.10.2016 um 18:10 schrieb William Dunlap:
>> Are you saying that
>>    f1 <- function(x) log(x)
>>    f2 <- function(x) { log } (x)
>> should act differently?
> yes. Or more precisely: I would expect that. "Should" implies,
that I want to change something. I just want to understand the behavior (or file
a bug, if this would have been one).

I think Bill and Luke are failing in trying to make you work out the logic for
yourself...

The point is that
{
  some_computation
}(x)

is an expression that evaluates some_computation and applies it as a function to
the argument x (or fails if not a function).

When you define functions, the body can be a single expression, so

f <- function(a)
{
  some_computation
}(x)

is effectively the same as

f <- function(a) {
 {
   some_computation
 }(x)
}

where you seem to be expecting

{f <- function(a) {
 {
   some_computation
 }
}(x)

Got it?

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com