Interpreting Simple Functions
Add an eval()
function that can evaluate a list, given
its internal representation. For now, just implement the basic functions quote, car, cdr, cons
and symbol?
.
All these functions are one to three lines long with the exception of
cons, which is perhaps 6 or 7 lines long. Each of these functions
(eval, car, cdr, quote
) should have a List
parameter (cons
has two) and return a List
.
When this phase is complete, a sample run might look like
this.
A few tips on evaluation:
quote, car, cdr
,
symbol?
and
cons
in the same file as the parser, since they have to look
into the implementation of the cons-cell struct. In fact, you could
also put eval()
in that same module. But even if you don't
get to it, think of some way of separating
the evaluation from the parsing as much as possible, by putting most
of the evaluation functions (including eval
) in a separate
module.(car (cdr (quote (a b c))))
"),
it should first evaluate the parameters (so in the cited example, we
first have to evaluate "(cdr (quote (a b c)))
"). Thus eval()
should
be recursive. It will bottom out when it gets to the level of a symbol or
an empty list.car
and
cdr
to get at the parameters in a function call.Submission location is here.
Back to CS170 Assignments.