File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #lang racket
2+
3+ ;; type Expr = Number
4+ ;; | Boolean
5+ ;; | (list Op1 Expr)
6+ ;; | (list Op2 Expr)
7+ ;; | (list 'if Expr Expr Expr)
8+ ;; | (list Expr Expr)
9+ ;; | (list 'λ (list Id) Expr)
10+ ;; | Id
11+
12+ ;; type Id = Symbol
13+ ;; type Op1 = 'sub1 | 'zero?
14+ ;; type Op2 = '+
15+
16+ ;; type Value = Number
17+ ;; | Boolean
18+ ;; | (Value -> Value)
19+
20+ ;; Expr Env -> Value
21+ (define (interp e r)
22+ (match e
23+ [(list '+ e1 e2)
24+ (+ (interp e1 r) (interp e2 r))]
25+ [(list 'sub1 e1)
26+ (sub1 (interp e1 r))]
27+ [(list 'zero? e1)
28+ (zero? (interp e1 r))]
29+ [(list 'if e1 e2 e3)
30+ (if (interp e1 r)
31+ (interp e2 r)
32+ (interp e3 r))]
33+ [(list 'λ (list x) e1)
34+ (λ (v) (interp e1 (cons (cons x v) r)))]
35+ [(list e1 e2)
36+ ((interp e1 r) (interp e2 r))]
37+ [_
38+ (if (symbol? e)
39+ (lookup e r)
40+ e)]))
41+
42+ ;; Id Env -> Value
43+ (define (lookup x r)
44+ (match r
45+ [(cons (cons y v) r)
46+ (if (eq? x y)
47+ v
48+ (lookup x r))]))
49+
50+ (interp '(((λ (t)
51+ ((λ (f) (t (λ (z) ((f f) z))))
52+ (λ (f) (t (λ (z) ((f f) z))))))
53+ (λ (tri)
54+ (λ (n)
55+ (if (zero? n)
56+ 0
57+ (+ n (tri (sub1 n)))))))
58+ 36 )
59+ '() )
You can’t perform that action at this time.
0 commit comments