Skip to content

Commit 6f90c79

Browse files
committed
finish hw5
1 parent 1d63c39 commit 6f90c79

File tree

2 files changed

+74
-59
lines changed

2 files changed

+74
-59
lines changed

hw5.rkt

+63-55
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222

2323
(define (racketlist->mupllist xs)
2424
(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+
)))
2828

2929
(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+
))
3434

3535
;; Problem 2
3636

3737
;; lookup a variable in an environment
3838
;; Do NOT change this function
3939
(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)]
4141
[(equal? (car (car env)) str) (cdr (car env))]
4242
[#t (envlookup (cdr env) str)]))
4343

@@ -59,64 +59,71 @@
5959
;; CHANGE add more cases here
6060
[(int? e) e]
6161
[(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")))]
7070
[(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))]
8087
[(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+
8592
[(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+
8996
[(closure? e) e]
90-
97+
9198
[(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)])
94101
(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+
104111
[(fun? e) (closure env e)]
105-
112+
106113
[#t (error (format "bad MUPL expression: ~v" e))]))
107114

108115
;; Do NOT change
109116
(define (eval-exp e)
110117
(eval-under-env e null))
111-
118+
112119
;; Problem 3
113120

114121
(define (ifaunit e1 e2 e3) (ifgreater (isaunit e1) (int 0) e2 e3))
115122

116123
(define (mlet* lstlst e2)
117124
(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))))
120127

121128
(define (ifeq e1 e2 e3 e4)
122129
(let ([v1 (var "_x")]
@@ -127,14 +134,15 @@
127134

128135
;; Problem 4
129136

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")) )))) ))
134141

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+
))
138146

139147
;; Challenge Problem
140148

hw5test.rkt

+11-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
;; call test
2929
(check-equal? (eval-exp (call (closure '() (fun #f "x" (add (var "x") (int 7)))) (int 1))) (int 8) "call test")
30+
31+
(check-equal? (eval-exp (call (fun #f "x" (add (var "x") (int 7))) (int 1))) (int 8) "call test2")
32+
33+
(check-equal? (eval-exp
34+
(call (fun "f" "x" (ifgreater (var "x") (int 0) (add (var "x") (call (var "f") (int 0))) (int 3)) ) (int 30)))
35+
(int 33) "call test3")
3036

3137
;;snd test
3238
(check-equal? (eval-exp (snd (apair (int 1) (int 2)))) (int 2) "snd test")
@@ -42,16 +48,17 @@
4248

4349
;; ifeq test
4450
(check-equal? (eval-exp (ifeq (int 1) (int 2) (int 3) (int 4))) (int 4) "ifeq test")
51+
(check-equal? (eval-exp (ifeq (int 2) (int 2) (int 3) (int 4))) (int 3) "ifeq test2")
4552

4653
;; mupl-map test
4754
(check-equal? (eval-exp (call (call mupl-map (fun #f "x" (add (var "x") (int 7)))) (apair (int 1) (aunit))))
4855
(apair (int 8) (aunit)) "mupl-map test")
4956

5057
;; problems 1, 2, and 4 combined test
51-
;;(check-equal? (mupllist->racketlist
52-
;;(eval-exp (call (call mupl-mapAddN (int 7))
53-
;; (racketlist->mupllist
54-
;; (list (int 3) (int 4) (int 9)))))) (list (int 10) (int 11) (int 16)) "combined test")
58+
(check-equal? (mupllist->racketlist
59+
(eval-exp (call (call mupl-mapAddN (int 7))
60+
(racketlist->mupllist
61+
(list (int 3) (int 4) (int 9)))))) (list (int 10) (int 11) (int 16)) "combined test")
5562

5663
))
5764

0 commit comments

Comments
 (0)