Internal Representation of Lists
Modify your S_Expression() function so that it returns
the underlying "cons-cell" structure of the list that it parses. For example,
if your interpreter reads the list (a b c)
, then
S_Expression() should return the structure:
To complement this, write a function printList()
that, given
a pointer to such a cons-cell structure, will print out the list in
parenthesized form. Thus, given a pointer to the above structure,
printList()
will print out:
(a b c)
Note that what you have now is a way to read the list in and
represent it as the cons-cell structure, and a way to write that cons-cell
structure back out so that the list looked like what you put in. This may
seem silly, but trust me... the alternative is an extremely unwieldy
interpreter. At this point, your interpreter should just take an
s-expression and echo it back out, as in the sample run
given right here. Of course, there
is a trivial way to get the same effect (simply echo the string),
but the only thing that matters in
this assignment is what happens "behind the scenes."
A few tips on the cons-cell structure:
List
, that is a pointer to
a cons-cell. If you work this right, you can "think in Scheme" as
you develop your interpreter. Although you're programming with pointers,
I'd like to see code with as few pointers (i.e.,"*" or "-->") in it as possible.#f
is synonymous with ()
, i.e., the empty list. Not all Schemes work
this way, but some do (MIT Scheme for example, as well as other LISPs such
as Common LISP). Thus #f
is the one object that is both a
symbol and a list. One sneaky
trick I have found useful is to create two "constant" cons-cells, one for
#t
and another for #f
, and use them universally
in your program to represent #t
and #f
.
This makes it easy to test to see
if a symbol or list is equal to one or the other, since you'll only need
"==
". '(a b c)
, a list structure that represents
the list using the quote
function, as in
(quote (a b c))
, is created. Don't do this unless you
really know what you're doing. Better to proceed to the
next assignment and return to it in a later project if you like.Submission location is here.
Back to CS170 Assignments.