From ggrothendieck at gmail.com  Sun Mar 30 19:51:45 2008
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Sun, 30 Mar 2008 13:51:45 -0400
Subject: [Rd] callCC in 2.7.0
In-Reply-To: <971536df0803301042m5fcb786bkbfa239d22ee73049@mail.gmail.com>
References: <971536df0803300710t40f61256x78b9889a5c323924@mail.gmail.com>
	<f8e6ff050803300904i1a950ed5r58738fb4184ccad0@mail.gmail.com>
	<971536df0803300934t4f354132od131d1f567652fd0@mail.gmail.com>
	<Pine.LNX.4.64.0803301215380.6663@itasca2.wildberry.org>
	<971536df0803301042m5fcb786bkbfa239d22ee73049@mail.gmail.com>
Message-ID: <971536df0803301051t65629860i31f1a724b5b72c3f@mail.gmail.com>

Sorry it should be as follows:

fib <- function(i, a = 0, b = 1) {
  if (i == 0) b else fib(i-1, b, a+b)
}

Now, how do we transform that to use callCC?


On Sun, Mar 30, 2008 at 1:42 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> OK. Can you show code to implement the tail recursive version of
> fib using callCC in R, say.
>
> Here it is transformed to tail recursive style:
>
> fib <- function(i, a = 0, b = 1) {
>  if (i == 0) a else fib(i-1, b, a+b)
>
> Now, how do I add callCC to all this so that the fib call
> presumably does not create a new stack instance?
>
>
> On Sun, Mar 30, 2008 at 1:31 PM, Luke Tierney <luke at stat.uiowa.edu> wrote:
> > On Sun, 30 Mar 2008, Gabor Grothendieck wrote:
> >
> > > I think the only relationship to that is the name since
> > > it does not appear to allow one to leave a function
> > > in the middle of its processing and re-enter it back
> > > at that point -- which is what would be needed.
> >
> > The article conflates basic CPS with having first class continuations
> > as in Scheme. The discussion about compilers and tail calls only
> > requires downward-only continuations of the kind provided by R's
> > current callCC.  The user interface and coroutine discussion requires
> > continuations that can be run outside of their creating context.  The
> > most sophisticated variant, as provided in Scheme, also allows
> > continuations to be run more than once.  I don't think any of the
> > examples in the Wikipedia article need that, but there is some
> > interesting work on using that to model web browsing behavior.
> >
> > At any rate, there is plenty of precedent for using callCC as the name
> > for the construct here even when the continuation is no longer valid
> > outside of the creating callCC call. So the relationship is more than
> > just the name.
> >
> > luke
> >
> > >
> > > On Sun, Mar 30, 2008 at 12:04 PM,  <h.wickham at gmail.com> wrote:
> > >>
> > >>> Would anyone like to explain if callCC in R 2.7.0 gives
> > >>> anything that on.exit does not already provide?
> > >>>
> > >>> It seems that the exit condition once defined cannot
> > >>> be added to overridden whereas with on.exit multiple
> > >>> on.exit's add additional on.exits rather than being ignored.
> > >>>
> > >>> Is this important?
> > >>
> > >> It facilitates a completely different style of programming - see
> > >> http://en.wikipedia.org/wiki/Continuation-passing_style
> > >>
> > >> --
> > >> http://had.co.nz/
> > >>
> > >
> >
> > > ______________________________________________
> > > R-devel at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> > >
> >
> > --
> > Luke Tierney
> > Chair, Statistics and Actuarial Science
> > Ralph E. Wareham Professor of Mathematical Sciences
> > University of Iowa                  Phone:             319-335-3386
> > Department of Statistics and        Fax:               319-335-3017
> >    Actuarial Science
> > 241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
> > Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
> >
>



[Rd] callCC in 2.7.0
Gabor Grothendieck