CS 3520 Homework 5   - Due September 26

This homework is due September 26, 11:59 PM. Place all of the requested functions in a single file, hw5.scm, and hand it in with submit on the CS filesystem.

Important: Start with your solution to HW 4, or use the official solution when it becomes available.

Exercise 5.1, New Primitives

Extend your interpreter from HW 4:
  1. Add a new primitive car to the language, which returns the left-hand side of a tree.
  2. Add a new primitive cdr to the language, which returns the right-hand side of a tree.
  3. Add a new primitive iscons to the language, which returns 1 when applied to a value created by cons, or 0 for any other argument

Here are some example programs to try with your interpreter:

Exercise 5.2, Procedures

Extend your interpreter by adding a simple proc form, which supports only one argument. Also, add a single-argument application form:

These forms work like the ones in the book language, but your implementation must not allow multiple arguments for a function.

Note that the set of expressed values now includes numbers, number trees, and procedures. Furthermore, with no extra effort, your language should support num-proc-trees:

<num-proc-tree> ::= <num>
                ::= <proc>
                ::= (cons <num-proc-tree> <num-proc-tree>)

Of course, operations such as + can assume that they will only receive num-tree arguments. But cons, car, cdr, and iscons must work on num-proc-trees. (Again, this extra support likely requires no effort on your part.)

Exercise 5.3, Recursion, the Hard Way

Using the language that you have implemented, write a program whose value is a procedure that behaves like heavier-tree in HW 4.

Express your answer through a function answer-for-5.3 that takes no arguments and returns a Scheme string. The content of the string should be the text of a program to implement heavier-tree.

For example, if this question had asked you to write the identity function, acceptable answers would include

(define (answer-for-5.3) "proc(x)x")

or

(define (answer-for-5.3) "let id = proc(x)x in id")

or even

(define (answer-for-5.3) "let id = proc(x)x in (id id)")

though the last one is obviously unnecessarily complex.

We will test your program like this:

(eval-program (scan&parse (string-append "(" (answer-for-5.3) " 1)")))
;; result should be 2
(eval-program (scan&parse (string-append "(" (answer-for-5.3) " cons(1,3))")))
;; result should be '(2 . 4)
(eval-program (scan&parse (string-append "(" (answer-for-5.3) " cons(1,cons(3,5)))")))
;; result should be '(2 . (4 . 6))

Hint 1: heavier-tree is a recursive function.

Hint 2: Your language does not include letrec.

Hint 3: See lecture 8.

Hint 4: Your language does not include multiple-argument procedures.

Hint 5: Consing can make two arguments one; currying can also avoid multiple arguments.


Last update: Tuesday, September 24th, 2002
mflatt@cs.utah.edu