Scheme from Scratch - Bootstrap v0.14 - Begin

The last few days have been some meaty implementation steps. Today is quite light with the implementation of the begin form.

Sample REPL session:

$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
> (begin 1 2 3)
3

The begin form doesn’t introduce a new lexical scope like a lambda does but begin does have a tail position that must be handled correctly.

As noted by readers in the comments below, a compound procedure has an implicit begin form for the body of the procedure. The procedure application portion of eval can be refactored to use this fact. I’ve made this change to the code. Perhaps introducing begin before lambda would have been a better pedagogical progression but I was getting impatient to get to lambda.

There is a v0.14 branch on github for this version.

Previous article: Lambda the Ultimate
Next article: Cond

Comments

Have something to write? Comment on this article.

Malika January 18, 2010

You actually already kind of implemented begin as it is strikingly similar to the body of the lambda. Might be worth refactoring the common piece out?

Peter Michaux January 18, 2010

Malika,

Perhaps but you would have to be quite careful about preserving the proper tail call and I think that might make the code a bit messy.

Matei Conovici January 18, 2010

One not so messy way is to transform the procedure body in apply into a begin sequence and tail call for that, with the extended environment.

Peter Michaux January 18, 2010

Matei,

That is a nice idea. I will make that change later today. Thanks.

Chris January 18, 2010

After spending a good chunk of the day fighting with bugs in my lambda and tail calling implementations. I added a primitive to dump the currently active environment. It extremely helpful in tracking down where my code was using the wrong environment. It also helps prove that tail calls are working correctly. ;)

Stu January 19, 2010

Oh so much to do. I’m still doing the final parts on v0.12 let alone lambda + begin :) I know my environments are wrong, but I’ll hack on that when I get to it.

It sure is getting more interesting (and harder as my scheme knowledge is about zero...)

Peter Michaux January 19, 2010

Stu,

There were a few big implementation days recently. I’ve tried to slow down the pace again to just one small form per day so folks can catch up. I have real work to do too. :-)

Have something to write? Comment on this article.