-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscmhelp.lsp
48 lines (37 loc) · 1.26 KB
/
scmhelp.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
; Stuff common to scheme.lsp and tail.lsp.
; Environment operations
(define extend-env
(lambda (vars vals env)
(cons (cons vars vals) env)))
(define lookup-variable
(lambda (var env)
(if (null? env)
(eval var) ; to give access to Lisp primitives and constants
(lookup-in-frame var (caar env) (cdar env) (cdr env)))))
(define lookup-in-frame
(lambda (var vars vals enclosing-env)
(if (null? vars)
(lookup-variable var enclosing-env)
(if (eq? (car vars) var)
(car vals)
(lookup-in-frame var (cdr vars) (cdr vals) enclosing-env)))))
(define define-variable-value
(lambda (var value env)
(let ((frame (car env)))
(set-car! frame (cons var (car frame)))
(set-cdr! frame (cons value (cdr frame))))))
; Syntax
(define cadar (lambda (lst) (cadr (car lst))))
(define cdddr (lambda (lst) (cddr (cdr lst))))
(define caddar (lambda (lst) (cadr (cdr (car lst)))))
(define test-exp cadr)
(define then-exp caddr)
(define else-exp (lambda (exp) (if (cdddr exp) (cadddr exp) nil)))
(define make-closure cons)
(define closure-formals cadar)
(define closure-body caddar)
(define closure-env cdr)
(define primitive?
(lambda (object)
(and (pair? object)
(eq? (car object) '%prim))))