[racket] Why begin0 doesn't expands to begin ?

From: Eric Dobson (eric.n.dobson at gmail.com)
Date: Sat Dec 15 00:57:13 EST 2012

begin0 is for exactly the case you ignore, multiple values. You cannot know
the number of values the first expression will return statically so if you
did a translation to begin it would need to allocate them in a list on the
heap and then use (apply values ...) later.

With begin0 as a primitive you just evaluate the expression, putting its
return values on the racket stack (the racket bytecode machinery is stack
based). You then continue with the rest of the expressions, ignoring their
return values, and then continue with the rest of the program with the
return values already in place.

Someone who understands the lower levels better than me might be able to
give a better answer.


On Fri, Dec 14, 2012 at 7:06 PM, Gustavo Massaccesi <gustavo at oma.org.ar>wrote:

> The "begin" and "begin0" forms appear in the racket programs, in the
> fully expanded programs and in the bytecode. I expected that in some
> intermediate expansion step the "begin0" form expands to a "begin"
> form.
>
> For example something like: (This version doesn't work for multiple
> return values.)
>
> (define-syntax-rule (my-begin0 first rest ...)
>   (let ([temp first])
>      rest ...
>     temp))
>
> With this expansion the number of different kinds of operations in the
> bytecode /virtual machine is smaller, so it would be easier to
> maintain and optimize.
>
> My question is: Is this a bad idea because there is a special
> optimization for the "begin0" form?
>
> Gustavo
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121214/59fb05fe/attachment-0001.html>

Posted on the users mailing list.