CSSE 304 Exam 2 Part 2b 10/18/2017 (day 26.5) Name______________________ Section: 01(9:00) 02 (9:55) 03 (10:50 TR)
Problem
Possible
Earned
Comments
C4
20
During the entire exam you may not use email, IM, cell phone, PDA, headphones, ear buds, or any other communication device or software that allows you to communicate with another person.
Part 2b enhance your interpreter. For this part, you may use a Scheme editing/programming environment, the textbooks from the course (including TSPL, of course), The Chez Scheme Users’ Guide, the PLC grading program, and any materials that I provided online for the course. You may not use any other web or network resources. You are also allowed to look at and copy any Scheme code that you (or you and your partner) wrote before the exam, but nothing else (except code in the resources listed above) that was written by others, including CSSE 304 students from previous terms.
For any code you are asked to write, you may assume that all input values will have correct types; you do not need to write code to check for illegal input data. Test your code off-line before you submit to the grading server (an attempt to reduce the load on the server).
Submit your code to the E2-201810-2b assignment on the PLC server. Before you leave, ask the TA in your classroom to check to make sure that your code can be downloaded (and unzipped if you submit a ZIP file).
When you have finished either of Part 2a or Part 2b, please go ahead and submit the paper for that part. Then we can go ahead and grade yours, increasing our chances of returning the exams in Thursday¡¯s class.
C4. (20 points) The Chez Scheme Users Guide describes trace-lambda in the following way: syntax: (trace-lambda name formals exp1 exp2 …)
returns: a traced procedure
A trace-lambda expression is equivalent to a lambda expression with the same formals and body, except that trace information is printed to the current output port whenever the procedure is invoked, using name to identify the procedure. The trace information shows the value of the arguments passed to the procedure and the values returned by the procedure, with indentation to show the nesting of calls.
What you are to implement in your interpreter is almost like Chez Scheme’s trace-lambda, except that the indentation of the tracing output will be simpler. Nested calls should be indented two character positions for each level of nesting, and the beginning of the output lines have “| ” for each indentation level. See the examples on the next page. This should be simpler to implement, and it should produce output that is easier to read than if your interpreter does exactly what Chez Scheme’s trace-lambda does. Furthermore, we are providing a procedure that handles the details of the indented output.
For simplicity, you are only required to handle ¡°normal and simple¡± lambdas: the list of formal arguments is a proper list and where that procedure has only one body: (trace-lambda name (var …) body).
You are allowed (but not required) to handle tracing of procedures that have multiple bodies if that makes it easier for you.
Mutation: Of course your interpreter will have to keep track of the current indentation level, and this may require mutation in your interpreter code. Mutation is allowed for this problem. One approach is to mutate a global variable.
Read this part carefully: it can save you a lot of time.
We have provided a procedure called display-traced-output which you can call when you need to produce a single line of tracing output. It produces output, and it does not return a value. It is likely that when you use it, you will put the procedure call inside a begin. You will want to call display-traced-output
(a) when entering a traced procedure (call it with 3 arguments: indent-level proc-name arg-values) and (b) when returning from a traced procedure (call it with 2 arguments: indent-level value).
We suggest that you load this procedure¡¯s definition and interactively make a couple of calls to it to test your understanding.
> (display-traced-output 5 ‘f ‘(a b c)) | | | | | (f a b c)
> (display-traced-output 3 4)
|| | 4
> (define args ‘(a b c))
> (display-traced-output 5 ‘f args) | | | | | (f a b c)
Partial Credit:
To help us give you partial credit if appropriate, help us find the code that you wrote specifically for this problem: In Every place where you modify your interpreter code, place a comment: ; change
Important notes on testing and submitting this problem.
1. You can start with your A13 interpreter if you wish. There should be nothing in our test code that uses A14 features.
2. Because the grading program does not handle output statements well, there are no tests for trace-lambda on the PLC grading server. You (and I)
will need to test this part by hand.
3. There is a E2-201810-2b assignment on the PLC server which is a place for you to submit your interpreter code, but it will not give you correct
feedback.
4. Suggestion: AS soon as you have submit your code, ask the TA or instructor in your classroom to make sure that we can download your code.
The next page contains examples.
This definition is part of the starting code that we will give you:
(define display-traced-output
(let ([multi-indent-string ; produce the proper string of “| ” ¡¯s
(lambda (level)
(let loop ([level level] [result “”])
(if (zero? level)
result
(loop (- level 1) (string-append result “| “)))))])
(lambda args ; (level proc-name args) or (level answer) (let ([indent-string (multi-indent-string (car args))])
(display indent-string)
(display (if (= 2(length args)) ; otherwise the length is 3
(cadr args) ; display the return value of a call (cons (cadr args) (caddr args))))) ; display the args
Trace-lambda examples
> (rep)
–>(let ([a (trace-lambda aa (n)
–>(let ([f1 (trace-lambda f1 (b c) (+ b c))] [f2 (trace-lambda f2 (x) (* x x ))])
(+ 1 n))])
(let ([b (trace-lambda bb (n m)
(let ([f3 (trace-lambda f3 (x y)
(f2 (f1 (+ x y) 1)))])
| (aa 3) |4
| (cc 4) | | (aa 4) | | 5
| | (bb 4 5)
| | | (aa 4)
| | | 5
| | 10
| 14 14
| (f4 2) ||(f32 3) |||(f1 51) | | | 6 |||(f2 6) | | | 36
| | 36
| | (f2 4)
| | 16
| | (f3 16 36)
| | | (f1 52 1)
| | | 53
| | | (f2 53)
| | | 2809
| | 2809
| 2809
| (f4 2809)
| | (f3 2809 3)
| | | (f1 2812 1)
| | | 2813
| | | (f2 2813)
| | | 7912969
| | 7912969
| | (f2 4)
| | 16
| | (f3 16 7912969)
| | | (f1 7912985 1)
| | | 7912986
| | | (f2 7912986)
| | | 62615347436196
| | 62615347436196
| 62615347436196
62615347436196
| (r 7)
| | (g 7 2) | | 14 ||(g141) | | 20
| 20
20
–>(let ([f1 [f2
(trace-lambda f1 (x y) (+ x y))]
(+ m (a n)))])
((trace-lambda cc (n)
(+ n (b n (a n))))
(let ([f4 (trace-lambda f4 (x)
(f3 (f2 4) (f3 x 3)))])
(a 3))))
(f4 (f4 2)))))
–>(let ([a 2])
(let ([g (let ([b (+ a 3)])
(trace-lambda g (x y)
(+ x y b)))])
((trace-lambda r (x) (g (g x 2) 1))
7)))))
(lambda (n) (+ n 3))]
[f3 (trace-lambda f3 (t) (+ 8 t))])
(f1 (f1 (f2 (f3 4)) (f2 5)) 6))
| (f3 4)
| 12
| (f1 15 8)
| 23
| (f1 23 6)
| 29
29
CSSE 304
Exam #2
10/17/17 Page 3 of 3