Scheme from Scratch - Bootstrap v0.6 - Pairs
After today’s additions, our pretty printing REPL will echo any dotted pairs, proper lists and improper lists we enter. For example:
$ ./scheme Welcome to Bootstrap Scheme. Use ctrl-c to exit. > (0 . 1) (0 . 1) > (0 1) (0 1) > (0 . (1 . ())) (0 1) > (0 . (1 . 2)) (0 1 . 2)
The reader has to grow by a reasonable amount to make all these pair and list variations possible. The interesting part is that the reader functions
read_pair become mutually recursive. Together they form a recursive-descent parser. I thought it was pretty neat to implement a recursive-descent parser, type arbitrarily nested data structures, and imagine the parser traversing the structure to build the internal model. (Another example of a recursive-descent parser is found in section 5.12 Complicated Declarations on page 122 of K&R 2e.)
The print layer also must be able to print arbitrarily nested data structures. The functions
write_pair are mutually recursive and mirror the relationship of the reader code.
There is a v0.6 branch on github for this version.
Have something to write? Comment on this article.