Skip to content

right assignment ("->") and functions

4 messages · Brian Ripley, Thomas Lumley, robin hankin

#
Hi everyone

check this out [R-1.7.0]:

R> f1 <- function(x){x^2}
R> f1 -> f2
R> f2(4)
[1] 16
R> 
R> function(x){x^2} -> f3
function(x){x^2} -> f3
R> f3(4)
Error: couldn't find function "f3"

Why does right assignment "->" work in the first but not the second
case?  Can anyone else reproduce this?
#
On Mon, 23 Jun 2003, Robin Hankin wrote:

            
Because the second is equivalent to 

function(x) f3 <- {x^2}

an anonymous function.  Using a right assignment affects the direction of
parsing, but only after it is encountered and R is parsed left to right.
#
On Mon, 23 Jun 2003, Robin Hankin wrote:

            
It does work.  It just doesn't do what you expect.

Suppose you typed
 {x^2} -> f3
This would assign x^2 to f3.

So
  function(x)
    {x^2} -> f3

is an anonymous function of one argument, which assigns the square of that
argument to the local variable f3.

To get what you wanted you would need

{function(x) x^2}-> f3

	-thomas
#
Hi again list.

Thanks for these replies.  I guess the issue is one of parsing units
being different from my expectations.  I saw a related issue in a
slightly different context a couple of days ago:


R> f1 <- function(a,b)
{a
-b}
R> f2 <- function(a,b)
{a-
 b}


...which (rightly) give very different results [the original bug took
me *hours* to find].

cheers

rksh