Scheme from Scratch - Bootstrap v0.18 - Apply

The apply form is an interesting beast in Scheme. It is a procedure so can be passed around and assigned to variables. It has a special tail call requirement that most other primitive procedure has and that needs to handled carefully.

Sample REPL session:

$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
> (apply + '(1 2 3))
6
> (apply + 1 2 '(3))
6
> (apply + 1 2 3 '())
6

The first example above is the important one to have. I implemented apply to allow the last two examples for the fun of the implementation.

In my implementation, I did set up apply as a primitive procedure but it is one where the C function for the primitive procedure should never be called. It is a regular primitive procedure because that means apply can be passed around in the Scheme programs. I implemented a small trick in eval to handle apply and have the tail call requirement satisfied. I don’t want to enter the world of trampolines for my primitive procedures.

I have a list of what remains. I looks like there are only about six more days of implementation for Bootstrap Scheme and then a wrap-up article.

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

Previous article: And and Or
Next article: Eval

Comments

Have something to write? Comment on this article.

Matei Conovici January 22, 2010

Hah, I used the exact same trick. I was wondering if there are other possibly more elegant ways.

Have something to write? Comment on this article.