|
22 | 22 |
|
23 | 23 | (define (racketlist->mupllist xs)
|
24 | 24 | (if (null? xs)
|
25 |
| - (aunit) |
26 |
| - (apair (car xs) (racketlist->mupllist (cdr xs)) |
27 |
| - ))) |
| 25 | + (aunit) |
| 26 | + (apair (car xs) (racketlist->mupllist (cdr xs)) |
| 27 | + ))) |
28 | 28 |
|
29 | 29 | (define (mupllist->racketlist xs)
|
30 |
| - (if (aunit? xs) |
31 |
| - null |
32 |
| - (cons (apair-e1 xs) (mupllist->racketlist (apair-e2 xs))) |
33 |
| - )) |
| 30 | + (if (aunit? xs) |
| 31 | + null |
| 32 | + (cons (apair-e1 xs) (mupllist->racketlist (apair-e2 xs))) |
| 33 | + )) |
34 | 34 |
|
35 | 35 | ;; Problem 2
|
36 | 36 |
|
37 | 37 | ;; lookup a variable in an environment
|
38 | 38 | ;; Do NOT change this function
|
39 | 39 | (define (envlookup env str)
|
40 |
| - (cond [(null? env) (error "unbound variable during evaluation" str)] |
| 40 | + (cond [(null? env) (error "unbound variable during evaluation" str env)] |
41 | 41 | [(equal? (car (car env)) str) (cdr (car env))]
|
42 | 42 | [#t (envlookup (cdr env) str)]))
|
43 | 43 |
|
|
59 | 59 | ;; CHANGE add more cases here
|
60 | 60 | [(int? e) e]
|
61 | 61 | [(ifgreater? e)
|
62 |
| - (let ([v1 (eval-under-env (ifgreater-e1 e) env)] |
63 |
| - [v2 (eval-under-env (ifgreater-e2 e) env)]) |
64 |
| - (if (and (int? v1) |
65 |
| - (int? v2)) |
66 |
| - (if (> (int-num v1) (int-num v2)) |
67 |
| - (eval-under-env (ifgreater-e3 e) env) |
68 |
| - (eval-under-env (ifgreater-e4 e) env)) |
69 |
| - (error "ifgreater applied to nom-number")))] |
| 62 | + (let ([v1 (eval-under-env (ifgreater-e1 e) env)] |
| 63 | + [v2 (eval-under-env (ifgreater-e2 e) env)]) |
| 64 | + (if (and (int? v1) |
| 65 | + (int? v2)) |
| 66 | + (if (> (int-num v1) (int-num v2)) |
| 67 | + (eval-under-env (ifgreater-e3 e) env) |
| 68 | + (eval-under-env (ifgreater-e4 e) env)) |
| 69 | + (error "ifgreater applied to nom-number")))] |
70 | 70 | [(fst? e)
|
71 |
| - (if (apair? e) |
72 |
| - (eval-under-env (apair-e1 (fst-e e)) env) |
73 |
| - (error "FST applied to non-pair"))] |
74 |
| - |
75 |
| - [(snd? e) (cond [(apair? (snd-e e)) (eval-under-env (apair-e2 (snd-e e)) env)] |
76 |
| - [#t (error "SND applied to non-pair")])] |
77 |
| - |
78 |
| - [(aunit? e) (int 1)] |
79 |
| - [(apair? e) (apair (eval-under-env (apair-e1) env) (eval-under-env (apair-e2) env))] |
| 71 | + (let ([pr (eval-under-env (fst-e e) env)]) |
| 72 | + (if (apair? pr) |
| 73 | + (eval-under-env (apair-e1 pr) env) |
| 74 | + (error "FST applied to non-pair" e pr)))] |
| 75 | + |
| 76 | + [(snd? e) |
| 77 | + (let ([pr (eval-under-env (snd-e e) env)]) |
| 78 | + (if (apair? pr) |
| 79 | + (eval-under-env (apair-e2 pr) env) |
| 80 | + (error "SND applied to non-pair" e pr)))] |
| 81 | + |
| 82 | + ;;[(snd? e) (cond [(apair? (snd-e e)) (eval-under-env (apair-e2 (snd-e e)) env)] |
| 83 | + ;; [#t (error "SND applied to non-pair")])] |
| 84 | + |
| 85 | + [(aunit? e) e] |
| 86 | + [(apair? e) (apair (eval-under-env (apair-e1 e) env) (eval-under-env (apair-e2 e) env))] |
80 | 87 | [(isaunit? e)
|
81 |
| - (if (aunit? (eval-under-env (isaunit-e e) env)) |
82 |
| - (int 1) |
83 |
| - (int 0))] |
84 |
| - |
| 88 | + (if (aunit? (eval-under-env (isaunit-e e) env)) |
| 89 | + (int 1) |
| 90 | + (int 0))] |
| 91 | + |
85 | 92 | [(mlet? e)
|
86 |
| - (let ([val (eval-under-env (mlet-e e) env)]) |
87 |
| - (eval-under-env (mlet-body e) (cons (cons (mlet-var e) val) env)))] |
88 |
| - |
| 93 | + (let ([val (eval-under-env (mlet-e e) env)]) |
| 94 | + (eval-under-env (mlet-body e) (cons (cons (mlet-var e) val) env)))] |
| 95 | + |
89 | 96 | [(closure? e) e]
|
90 |
| - |
| 97 | + |
91 | 98 | [(call? e)
|
92 |
| - (let ([mclosure (eval-under-env (call-funexp e) env)] |
93 |
| - [arg (eval-under-env (call-actual e) env)]) |
| 99 | + (let ([mclosure (eval-under-env (call-funexp e) env)] |
| 100 | + [arg (eval-under-env (call-actual e) env)]) |
94 | 101 | (if (closure? mclosure)
|
95 |
| - (let* ([mfun (closure-fun mclosure)] |
96 |
| - [mclosure-env (closure-env mclosure)] |
97 |
| - [body (fun-body mfun)] |
98 |
| - [forma (fun-formal mfun)]) |
99 |
| - (if (string? (fun-nameopt mfun)) |
100 |
| - (eval-under-env (mlet forma arg (mlet (fun-nameopt mfun) mfun body)) env) |
101 |
| - (eval-under-env (mlet forma arg body) env))) |
102 |
| - (error "vall applied to non-closour")))] |
103 |
| - |
| 102 | + (let* ([mfun (closure-fun mclosure)] |
| 103 | + [mclosure-env (closure-env mclosure)] |
| 104 | + [body (fun-body mfun)] |
| 105 | + [forma (fun-formal mfun)]) |
| 106 | + (if (string? (fun-nameopt mfun)) |
| 107 | + (eval-under-env (mlet forma arg (mlet (fun-nameopt mfun) mclosure body)) mclosure-env) |
| 108 | + (eval-under-env (mlet forma arg body) mclosure-env))) |
| 109 | + (error "vall applied to non-closour")))] |
| 110 | + |
104 | 111 | [(fun? e) (closure env e)]
|
105 |
| - |
| 112 | + |
106 | 113 | [#t (error (format "bad MUPL expression: ~v" e))]))
|
107 | 114 |
|
108 | 115 | ;; Do NOT change
|
109 | 116 | (define (eval-exp e)
|
110 | 117 | (eval-under-env e null))
|
111 |
| - |
| 118 | + |
112 | 119 | ;; Problem 3
|
113 | 120 |
|
114 | 121 | (define (ifaunit e1 e2 e3) (ifgreater (isaunit e1) (int 0) e2 e3))
|
115 | 122 |
|
116 | 123 | (define (mlet* lstlst e2)
|
117 | 124 | (if (null? lstlst)
|
118 |
| - e2 |
119 |
| - (mlet (car (car lstlst)) (cdr (car lstlst)) (mlet* (cdr lstlst) e2)))) |
| 125 | + e2 |
| 126 | + (mlet (car (car lstlst)) (cdr (car lstlst)) (mlet* (cdr lstlst) e2)))) |
120 | 127 |
|
121 | 128 | (define (ifeq e1 e2 e3 e4)
|
122 | 129 | (let ([v1 (var "_x")]
|
|
127 | 134 |
|
128 | 135 | ;; Problem 4
|
129 | 136 |
|
130 |
| -;;(define mupl-map |
131 |
| -;; (fun #f "f" |
132 |
| -;; (fun "mfun" "xs" |
133 |
| -;; (ifaunit (var "xs") (aunit) (apair (call (var "mfun") (fst (var "xs"))) (call (var "f") (snd (var "xs")) )))) )) |
| 137 | +(define mupl-map |
| 138 | + (fun #f "f" |
| 139 | + (fun "mfun" "xs" |
| 140 | + (ifaunit (var "xs") (aunit) (apair (call (var "f") (fst (var "xs"))) (call (var "mfun") (snd (var "xs")) )))) )) |
134 | 141 |
|
135 |
| -;;(define mupl-mapAddN |
136 |
| - ;; (mlet "map" mupl-map |
137 |
| - ;; "CHANGE (notice map is now in MUPL scope)")) |
| 142 | +(define mupl-mapAddN |
| 143 | + (mlet "map" mupl-map |
| 144 | + (fun #f "add" (call (var "map") (fun #f "x" (add (var "x") (var "add"))))) |
| 145 | + )) |
138 | 146 |
|
139 | 147 | ;; Challenge Problem
|
140 | 148 |
|
|
0 commit comments