Scheme from Scratch - Bootstrap v0.10 - If
if form is the most basic conditional form in Scheme. The
case forms can be reduced to a series of
A sample REPL session:
$ ./scheme Welcome to Bootstrap Scheme. Use ctrl-c to exit. > (if #t 1 2) 1 > (if #t 'a 'b) a > (if #f 1 2) 2 > (if #t 1) 1 > (if #f 1) #f > (if 0 1 2) 1
Yes the last example is correct. In Scheme, all values are considered true except for
When you look in the implementation, you may gasp when you see the
goto is used to implement the tail expressions as discussed in section 3.5 of R5RS. It is possible to use a
while (1) loop instead of the
goto but I find that
goto describes the semantics of a tail call better especially with a label
tailcall. Feel free to do it either way in your code, of course.
There is a v0.10 branch on github for this version.
Have something to write? Comment on this article.