Skip to content

Commit ca46fed

Browse files
super zozosuper zozo
super zozo
authored and
super zozo
committed
update compute-free-var hw5
1 parent 40ba2c2 commit ca46fed

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

hw5.rkt

+21-15
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,24 @@
147147

148148
;; We will test this function directly, so it must do
149149
;; as described in the assignment
150-
(define (free-vars-helper e not-free)
151-
;;(println not-free)
152-
;;(print e)
153-
(let ([f (lambda (g e) (free-vars-helper (g e) not-free))])
150+
(define (free-vars-helper e)
151+
(let ([f (lambda (g e) (free-vars-helper (g e)))])
154152
(cond [(fun? e)
155153
(let* ([arg (fun-formal e)]
156154
[body (fun-body e)]
157-
[s (free-vars-helper body (set-add not-free arg))])
158-
(cons (fun-challenge (fun-nameopt e) arg (car s) (cdr s)) (cdr s)))]
155+
[s (free-vars-helper body)]
156+
[freevars (set-remove (cdr s) arg)])
157+
(cons (fun-challenge (fun-nameopt e) arg (car s) freevars) freevars))]
159158
[(var? e)
160-
(cons e (if (set-member? not-free (var-string e)) (set) (set (var-string e))))]
159+
(cons e (set (var-string e)))]
161160
[(mlet? e)
162161
(let* ([name (mlet-var e)]
163162
[val (mlet-e e)]
164163
[body (mlet-body e)]
165-
[s1 (free-vars-helper val not-free)]
166-
[s2 (free-vars-helper body (set-add not-free name))])
164+
[s1 (free-vars-helper val)]
165+
[s2 (free-vars-helper body)])
167166
(cons (mlet name (car s1) (car s2))
168-
(set-union (cdr s1) (cdr s2))))]
167+
(set-union (cdr s1) (set-remove (cdr s2) name))))]
169168
[(add? e)
170169
(let ([s1 (f add-e1 e)]
171170
[s2 (f add-e2 e)])
@@ -203,15 +202,22 @@
203202

204203

205204
(define (compute-free-vars e)
206-
(car (free-vars-helper e (set))))
205+
(car (free-vars-helper e)))
207206

208207

209208

210209

211210
;; Do NOT share code with eval-under-env because that will make
212211
;; auto-grading and peer assessment more difficult, so
213212
;; copy most of your interpreter here and make minor changes
213+
214+
(define (compute-env env freevars)
215+
(set-map freevars (lambda (x) (cons x (envlookup env x)))))
216+
217+
214218
(define (eval-under-env-c e env)
219+
(println e)
220+
(println env)
215221
(cond [(var? e)
216222
(envlookup env (var-string e))]
217223
[(add? e)
@@ -257,17 +263,17 @@
257263
(if (var? fn-call)
258264
(cons (cons (var-string fn-call) clojure) clojure-env) ;; Add bindings for recursive call
259265
clojure-env))]
260-
[arg-name (fun-formal fn)]
261-
[fn-body (fun-body fn)])
262-
(eval-under-env-c fn-body (cons (cons arg-name arg) envi)))]
266+
[reduced-envi (compute-env envi (fun-challenge-freevars fn))]
267+
[arg-name (fun-challenge-formal fn)]
268+
[fn-body (fun-challenge-body fn)])
269+
(eval-under-env-c fn-body (cons (cons arg-name arg) reduced-envi)))]
263270
[(apair? e)
264271
(apair (eval-under-env-c (apair-e1 e) env) (eval-under-env-c (apair-e2 e) env))]
265272
[(fst? e) (let ([pair (eval-under-env-c (fst-e e) env)])
266273
(if (apair? pair) (apair-e1 pair) (error "MUPL fst applied to non-pair")))]
267274
[(snd? e) (let ([pair (eval-under-env-c (snd-e e) env)])
268275
(if (apair? pair) (apair-e2 pair) (error "MUPL snd applied to non-pair")))]
269276
[(isaunit? e) (if (aunit? (eval-under-env-c (isaunit-e e) env)) (int 1) (int 0))]
270-
;;
271277
[#t (error (format "bad MUPL expression: ~v" e))]))
272278

273279
;; Do NOT change this

hw5test.rkt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#lang racket
2+
;; Programming Languages Homework 5 Simple Test
3+
;; Save this file to the same directory as your homework file
4+
;; These are basic tests. Passing these tests does not guarantee that your code will pass the actual homework grader
5+
6+
;; Be sure to put your homework file in the same folder as this test file.
7+
;; Uncomment the line below and, if necessary, change the filename
8+
;;(require "hw5")
9+
10+
(require rackunit)
11+
12+
(define tests
13+
(test-suite
14+
"Sample tests for Assignment 5"
15+
16+
;; check racketlist to mupllist with normal list
17+
(check-equal? (racketlist->mupllist (list (int 3) (int 4))) (apair (int 3) (apair (int 4) (aunit))) "racketlist->mupllist test")
18+
19+
;; check mupllist to racketlist with normal list
20+
(check-equal? (mupllist->racketlist (apair (int 3) (apair (int 4) (aunit)))) (list (int 3) (int 4)) "racketlist->mupllist test")
21+
22+
;; tests if ifgreater returns (int 2)
23+
(check-equal? (eval-exp (ifgreater (int 3) (int 4) (int 3) (int 2))) (int 2) "ifgreater test")
24+
25+
;; mlet test
26+
(check-equal? (eval-exp (mlet "x" (int 1) (add (int 5) (var "x")))) (int 6) "mlet test")
27+
28+
;; call test
29+
(check-equal? (eval-exp (call (closure '() (fun #f "x" (add (var "x") (int 7)))) (int 1))) (int 8) "call test")
30+
31+
;;snd test
32+
(check-equal? (eval-exp (snd (apair (int 1) (int 2)))) (int 2) "snd test")
33+
34+
;; isaunit test
35+
(check-equal? (eval-exp (isaunit (closure '() (fun #f "x" (aunit))))) (int 0) "isaunit test")
36+
37+
;; ifaunit test
38+
(check-equal? (eval-exp (ifaunit (int 1) (int 2) (int 3))) (int 3) "ifaunit test")
39+
40+
;; mlet* test
41+
(check-equal? (eval-exp (mlet* (list (cons "x" (int 10))) (var "x"))) (int 10) "mlet* test")
42+
43+
;; ifeq test
44+
(check-equal? (eval-exp (ifeq (int 1) (int 2) (int 3) (int 4))) (int 4) "ifeq test")
45+
46+
;; mupl-map test
47+
(check-equal? (eval-exp (call (call mupl-map (fun #f "x" (add (var "x") (int 7)))) (apair (int 1) (aunit))))
48+
(apair (int 8) (aunit)) "mupl-map test")
49+
50+
;; 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")
55+
56+
))
57+
58+
(require rackunit/text-ui)
59+
;; runs the test
60+
(run-tests tests)

0 commit comments

Comments
 (0)