What you should try to do here is to understand how evaluation works by writing a very simple evaluator. For example (and this is pseudocode!):
(define (self-evaluating? form)
;; This might not be exactly right, might be missing one type or two,
;; but you get the idea.
(or (null? form)
(number? form)
(boolean? form)
(string? form)
(vector? form)
(character? form)))
(define (eval form env)
(cond ((self-evaluating? form)
;; Self-evaluating forms evaluate to themselves.
form)
((symbol? form)
;; A symbol is evaluating by looking it up in the environment.
;; Note that this is pseudocode, and lookup-in-environment is not
;; a standard function...
(lookup-in-environment form env))
((list? form)
(eval-combination form env))))
(define (eval-combination form env)
(case (car form)
((quote)
;; A quote special form is evaluated simply by returning the
;; argument to quote.
(second form))
((define)
;; We evaluate a definition by evaluating the body in the current
;; environment, and setting the variable to the result in that
;; environment.
;;
;; Note again that this is pseudocode, and set-in-environment! is
;; not a standard function...
(set-in-environment! env (second form) (eval (third form) env)))
;; Other special forms
...
;; Default rule: evaluate all the subexpressions in the current
;; environment; the first one should be a procedure, which we then
;; apply to the list of values of the succeeding ones.
(else
(apply (eval (car form) env)
(map (lambda (arg) (eval arg env)) (cdr form)))))))
By tracing the execution of that code by hand for a few examples (and you can ignore the env
parameter for your case), you should be able to see what's going on. The key thing to note about an evaluator like this is that it's very orthogonal: for each type of form there is a separate rule on how to evaluate it, and the rules don't know about each other. It's all driven by this logic:
- Is the form an atom or a combination (list)?
- If the form is an atom, is it self-evaluating or is it a symbol?
- If the form is a combination, is it a special form or a procedure application? The only thing we look at to decide this is the form's
car
.