[plt-scheme] begin0 and tail recursion

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sun Jul 20 10:05:06 EDT 2003

Oh, I should mention check syntax. Try checking the syntax of the
original program and this program:

(define (faki n)
  (define (iter n res)
    (if (= n 0)
        (continuation-mark-set->list
         (current-continuation-marks) 'key)
        (with-continuation-mark 'key (gensym 'env)
          (begin 
            'pop
            (iter (- n 1) (* n res))))))
  (iter n 1))


Move your mouse over the open paren before the define of iter. You'll
purple arrows that show you the syntactic tail call behavior of the
program.

Robby

At Sun, 20 Jul 2003 08:59:37 -0500, Robby Findler wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> It isn't a tail call because it must evaluate E1, then evaluate E2, and
> then return E1's result.
> 
> Robby
> 
> At Sun, 20 Jul 2003 16:58:12 +0000, "Saint Katsmall T. Wise, Esquire" wrote:
> > ------------------------------------------------------------------------------
> > It doesn't tail call because it evaluates x first, right?
> > 
> > Robby Findler wrote:
> > 
> > >  For list-related administrative tasks:
> > >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > >
> > >begin0 doesn't tail call any thing. 
> > >
> > >(begin0 E1 E2) = (let ([x E1]) (begin E2 x))
> > >
> > >Robby
> > >
> > >At Sun, 20 Jul 2003 15:36:34 +0200 (MEST), Stefan Ottosson wrote:
> > >  
> > >
> > >>  For list-related administrative tasks:
> > >>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > >>
> > >>Hi,
> > >>
> > >>I'm probably being naive, but shouldn't the following code be tail
> > >>recursive? It seems it isnt, unless you remove 'pop from the second
> > >>last line.
> > >>
> > >>(define (faki n)
> > >>  (define (iter n res)
> > >>    (if (= n 0)
> > >>        (continuation-mark-set->list
> > >>                     (current-continuation-marks) 'key)
> > >>        (with-continuation-mark 'key (gensym 'env)
> > >>                                (begin0 (iter (- n 1) (* n res)) 'pop))))
> > >>  (iter n 1))
> > >>
> > >>(faki 3) returns (env32208 env32207 env32206) as written above. If I
> > >>erase 'pop then (faki 3) returns just (env32211).
> > >>
> > >>    
> > >>
> > >
> > >
> > >  
> > >
> > 
> > ------------------------------------------------------------------------------
> > It doesn't tail call because it evaluates x first, right?
> > Robby Findler wrote:
> > 
> >   For list-related administrative tasks:
> > 
> >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > 
> > 
> > 
> > begin0 doesn't tail call any thing. 
> > 
> > 
> > 
> > (begin0 E1 E2) = (let ([x E1]) (begin E2 x))
> > 
> > 
> > 
> > Robby
> > 
> > 
> > 
> > At Sun, 20 Jul 2003 15:36:34 +0200 (MEST), Stefan Ottosson wrote:
> > 
> >   
> > 
> >   For list-related administrative tasks:
> > 
> >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > 
> > 
> > 
> > Hi,
> > 
> > 
> > 
> > I'm probably being naive, but shouldn't the following code be tail
> > 
> > recursive? It seems it isnt, unless you remove 'pop from the second
> > 
> > last line.
> > 
> > 
> > 
> > (define (faki n)
> > 
> >   (define (iter n res)
> > 
> >     (if (= n 0)
> > 
> >         (continuation-mark-set->list
> > 
> >                      (current-continuation-marks) 'key)
> > 
> >         (with-continuation-mark 'key (gensym 'env)
> > 
> >                                 (begin0 (iter (- n 1) (* n res)) 'pop))))
> > 
> >   (iter n 1))
> > 
> > 
> > 
> > (faki 3) returns (env32208 env32207 env32206) as written above. If I
> > 
> > erase 'pop then (faki 3) returns just (env32211).
> > 
> > 
> > 
> >     
> > 
> > 
> > 
> > 
> >   
> > 
> > ------------------------------------------------------------------------------
> 
> 



Posted on the users mailing list.