I'll answer anyway, because the `begin0` difference is subtle.

At the level of `let`, the Racket optimzier goes out of its way to not
convert your second program to the first. Continuation marks make a
change from non-tail call to tail call observable, and in this case,
the compiler won't be able to prove that there are no continuation-mark
observations that could be affected by the conversion.

For `begin0`, we have defined the first subexpression of `begin0` to be
in tail position with respect to the `begin0` form if it's the only
subexpression. That choice accommodates exactly this kind of case,
although it's a little ad hoc.

At Mon, 1 Oct 2018 11:37:29 -0700 (PDT), Phil Nguyen wrote:
> Actually nvm. Whoever wrote such macro could have just used `begin0` 
> instead.
> 
> On Monday, October 1, 2018 at 2:34:23 PM UTC-4, Phil Nguyen wrote:
> >
> > (Racket 7.0) This program runs in no time:
> >
> > (define (sum n acc)
> >   (if (> n 0)
> >       (sum (- n 1) (+ acc n))
> >       acc))
> >
> > (collect-garbage) (collect-garbage) (collect-garbage)
> > (time (sum 10000000 0))
> > ; cpu time: 47 real time: 57 gc time: 0
> > ; 50000005000000
> >
> > This slightly modified program runs significantly slower:
> >
> > (define (sum n acc)
> >   (if (> n 0)
> >       (let ([ans (sum (- n 1) (+ acc n))])
> >         ans)
> >       acc))
> >
> > (collect-garbage) (collect-garbage) (collect-garbage)
> > (time (sum 10000000 0))
> > ; cpu time: 1719 real time: 1872 gc time: 1064
> > ; 50000005000000
> >
> > While the call to `sum` in the second program is not in tail position, I 
> > wonder if this kind of code happens often enough from macro expansion to 
> > benefit from an optimization.
> >
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to