From ggrothendieck at gmail.com  Tue Sep 12 07:57:22 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Tue, 12 Sep 2006 01:57:22 -0400
Subject: [Rd] unexpected behaviour when defining a function
In-Reply-To: <971536df0609112255s5d46b1abu35720fe5264c69a2@mail.gmail.com>
References: <eb555e660609111626t3f03dd69q2e3ebfe845f6540c@mail.gmail.com>
	<971536df0609112255s5d46b1abu35720fe5264c69a2@mail.gmail.com>
Message-ID: <971536df0609112257l73b7f266s2c21b047ceb6f058@mail.gmail.com>

Sorry I forgot one of the dots.  Here it is corrected:

> bar <- function() 1
> foo <- function(bar. = bar()) {
+      bar.
+ }
> foo()
[1] 1
> foo(bar = bar)
function() 1

On 9/12/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> You can't have x=x in an argument list.
>
> Try the following noting that we put a dot at the end of bar:
>
> > bar <- function() 1
> > foo <- function(bar. = bar()) {
> +      bar
> + }
> > foo()
> function() 1
> > foo(bar = bar)
> function() 1
>
> On 9/11/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
> > Hi,
> >
> > I know S manuals used to warn against using the same names for a
> > variable and a function, but I have never seen that cause problems in
> > R, so I usually don't pay much attention to it. Which is why the
> > following behaviour came as a surprise:
> >
> > > bar <- function() 1
> > > foo <- function(bar = bar()) {
> > +     bar
> > + }
> > > foo(9)
> > [1] 9
> > > foo()
> > Error in foo() : recursive default argument reference
> >
> > Exactly what rule am I violating here?
> >
> > The following gives a slightly different error, but I assume it has a
> > similar origin:
> >
> > bar <- function() 1
> > foo <- function(bar) {
> >    if (missing(bar)) bar <- bar()
> >    bar
> > }
> > foo()
> >
> > This version works fine though (so the rule probably involves function
> > arguments somehow):
> >
> > foo <- function(baz) {
> >    if (missing(baz)) {
> >        baz <- function() 2
> >        baz <- baz()
> >    }
> >    baz
> > }
> > foo()
> >
> > -Deepayan
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>




[Rd] unexpected behaviour when defining a function
Gabor Grothendieck