Cover of "Structure and Interpretation of Computer Programs", JS adaptation
Base cases:
Recursive calls:
Base cases:
Recursive calls:
The scheme_eval
function chooses behavior based on expression form:
(<operator> <operand 0> ... <operand k>)
The special forms can all be identified by the first element:
(if <predicate> <consequent> <alternative>)
(lambda (<formal-parameters>) <body>)
(define <name> <expression>)
Any combination that is not a known special form must be a call expression.
(define (demo s)
(if (null? s)
'(3)
(cons (car s) (demo (cdr s))))) ; Special!
(demo (list 1 2)) ; Call expression!
Logical forms are special forms that may only evaluate some sub-expressions.
(if <predicate> <consequent> <alternative>)
(and <e1> ... <en>)
, (or <e1> ... <en>)
(cond (<p1> <e1>) ... (<pn> <en>) (else <e>))
The value of an if
expression is the value of a sub-expression:
'<expression>
is shorthand for (quote <expression>)
'(1 2)
is equivalent to (quote (1 2))
The scheme_read
parser converts '
to a combination that starts with quote
.
The quote
special form evaluates to the quoted expression, which is not evaluated.
(quote (+ 1 2))
evaluates to the three-element Scheme list
(+ 1 2)
A frame represents an environment by having a parent frame.
A frame is an instance of a Frame
object,
which has lookup
and define
methods.
In this interpreter, frames do not hold return values.
y | 3 | |
z | 5 |
x | 2 | |
z | 4 |
Define binds a symbol to a value in the first frame of the current environment.
(define <name> <expression>)
<expression>
<name>
to its value in the current frame
(define x (+ 1 2))
Procedure definition is shorthand of define with a lambda expression.
(define (<name> <formal parameters>) <body>)
(define <name> (lambda (<formal parameters>) <body>))
Lambda expressions evaluate to user-defined procedures
(lambda (<formal-parameters>) <body> ... )
class LambdaProcedure:
def __init__(self, formals, body, env):
self.formals = formals # A scheme list of symbols
self.body = body # A scheme list of expressions
self.env = env # A Frame instance
(lambda (x y) (* x y))
To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure.
Evaluate the body of the procedure in the environment that starts with this new frame.
(define (demo s)
(if (null? s)
'(3)
(cons (car s) (demo (cdr s)))))
(demo (list 1 2))