77
88@bold{Due: Friday, November 13th 11:59PM}
99
10- @(define repo "TBD " )
10+ @(define repo "https://classroom.github.com/a/Gv1cRIkD " )
1111
1212Midterm repository:
1313@centered{@link[repo repo]}
@@ -60,7 +60,7 @@ answer.
6060(define (fact x)
6161 (if (<= x 1 )
6262 1
63- (* x (sub1 x))))
63+ (* x (fact ( sub1 x) ))))
6464)
6565
6666How about this one? Again, justify your answer.
@@ -82,7 +82,7 @@ Hint: consider Jig.
8282[8 points]
8383
8484For each of the following expressions, which subexpressions are in tail
85- position?
85+ position? Assume that the top-level expression is in tail position.
8686
8787@itemlist[
8888
@@ -208,9 +208,53 @@ Would produce something like:
208208 (f (g 10 )))
209209)
210210
211+ Why should you care about @tt{fvs} or @tt{barendregtify}? Well, consider the
212+ following inlining:
211213
212- In addition to your code, make sure you answer the following question as part of your
213- submission:
214+ @#reader scribble/comment-reader
215+ (racketblock
216+ (begin
217+ (define (f x) (let ((y 5 )) x))
218+ (let ((y 42 )) (f y)))
219+ )
220+
221+ If you inline @tt{f} without care, you would get the following:
222+
223+ @#reader scribble/comment-reader
224+ (racketblock
225+ (begin
226+ (define (f x) (let ((y 5 )) x))
227+ (let ((y 42 )) (let ((y 5 )) y)))
228+ )
229+
230+ Now you've changed the program, it would result in 5 instead of 42!
231+ You can use @tt{fvs} to figure out which variables might be captured,
232+ and deal with that, or you can use @tt{barendregtify} to make sure
233+ that all variables are unique. Notice how if we had used @tt{barendregtify}
234+ we could have avoided the problem:
235+
236+ @#reader scribble/comment-reader
237+ (racketblock
238+ (begin
239+ (define (f var1) (let ((var2 5 )) var1))
240+ (let ((var3 42 )) (f var3)))
241+ )
242+
243+ Then inlining @tt{f} we get:
244+
245+ @#reader scribble/comment-reader
246+ (racketblock
247+ (begin
248+ (define (f var1) (let ((var2 5 )) var1))
249+ (let ((var3 42 )) (let ((var2 5 )) var3)))
250+ )
251+
252+ You may have to run/call @tt{barendregtify} more than once to ensure
253+ correctness, when inlining functions.
254+
255+
256+ In addition to your code, make sure you answer the following question as part
257+ of your submission:
214258
215259@itemlist[
216260
@@ -231,46 +275,30 @@ Other things you should consider:
231275
232276]
233277
278+ Part A:
234279
235-
236- @subsection{Example 1 }
280+ 5 points from the total of 20 will be given if you can produce the correct
281+ inlining of @tt{f} for the following program (you are free to do this by hand):
237282
238283@#reader scribble/comment-reader
239284(racketblock
240- ;todo
241- )
242-
243- Would be unchanged.
244-
245- @subsection{Example 2 }
246-
247- @#reader scribble/comment-reader
248- (racketblock
249- ;todo
250- )
251-
252- Becomes:
253-
254- @#reader scribble/comment-reader
255- (racketblock
256- ;todo
285+ (begin
286+ (define (f x) (let ((y (+ x 10 ))) (let ((z 42 )) (+ x (+ y z)))))
287+ (define (g x) (f x))
288+ (let ((y 1024 )) (g (f y))))
257289)
258290
259- @subsection{Example 3 }
291+ Part B:
260292
261- Don 't forget about nested expressions!
293+ 15 points for completing the implementation below:
262294
263295@#reader scribble/comment-reader
264296(racketblock
265- ;todo
297+ (define (inline f p)
298+ (match p
299+ [(prog ds e) ;TODO]))
266300)
267301
268- Becomes:
269-
270- @#reader scribble/comment-reader
271- (racketblock
272- ;todo
273- )
274302
275303@bold{Question 5 Extra Credit}
276304
0 commit comments