# R-alpha: Puzzles from Luke Tierney

Ross Ihaka (ihaka@stat.auckland.ac.nz)
Thu, 30 May 1996 13:11:26 +1200

Date: Thu, 30 May 1996 13:11:26 +1200
From: Ross Ihaka <ihaka@stat.auckland.ac.nz>
Message-Id: <199605300111.NAA04686@stat.auckland.ac.nz>
To: r-testers@stat.math.ethz.ch
Subject: R-alpha: Puzzles from Luke Tierney

Luke sent me these bugs directly (he's so full of good news :-).
I thought they were interesting enough for a wider audience.

We've now fixed these, but I have a sinking feeling that we're rapidly
running out of fingers and toes to plug up the holes in this
particular dike.

----------------------------------------------------------------------------

Luke Tierney writes:
> > 2) Recognize that x[1] <- 3 is a mutation of a local object.  If there
> >    is no "x" in the current evaluation frame, find an x from somewhere
> >    else and make a local copy of it.  Then continue with the mutation.
> >    This is in the spirit of what we do at present.  The check for a
> >    local "x" adds a small cost.  This will maintain compatibility with S.
>
> THis description made me try a couple of things:
>
> R : Copyright (c) 1996, Robert Gentleman and Ross Ihaka
> Version 0.6, (Released: May 17, 1996)
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
>
> > f<-function(x=x)x[1]<-3
> > f()
> Error: stack overflow

This is sort of what I said about "local x=x", but assumes the rhs x
is local.  Very much a chicken and egg problem.  It seems that S does
a check for this.

f<-function(x=x)x[1]<-3
Warning messages:
Function body is an assignment
(won't have any effect)
in: x[1] <- 3
> f()
Error in f(): Recursive occurrence of default argument "x"
Dumped
> f(10)
[1] 3

We now catch this too.

> f<-function(x=x)x[1]<-3
> f()
Error in f() : recursive default argument reference

-----------------------------------------------------------------------------

> > f<-function(y) { y[1]<-3; x<<-y; x[2]<-4;}
> > f(c(1,2))
> > x
> [1] 3 4  # in S it's 3 2

This the same as yesterday's problem.  There is no local "x" to
mutate, so the global one gets clobbered.  Since it is easiest :-) we
will probably make this an error.  This is incompatible behavior, and
if there is strong sentiment we will do the "make a local copy first"
thing to get compatibility.

-----------------------------------------------------------------------------

> > x<-list(y=NULL)
> > x\$y<-x
> > x
> \$...
> \$...
> .........
> Segmentation fault (core dumped)
>

This was an oversight in the mutation code.  If you instead try

x\$[[1]]<-x

You'll find it works.  The \$ case is (more) fixed now.

-----------------------------------------------------------------------------

I'm looking at other bug reports now ...

Ross
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-