Homework #6

Assigned: March 10
Due: March 17, 4:30pm


  1. (20 points) [EOPL] Exercise 3.23 on p. 90. Name the top-level function for your lexical-address calculator lexical-address and define it to accept an expression instead of a program.

  2. (15 points) [EOPL] Exercise 3.24 on p. 90. Name your lexical address calculator lexical-address and invoke it from the run procedure as follows:
    (define run
      (lambda (string)
        (eval-program (lexical-address (scan&parse string)))))
    
    Also, use the following definition of apply-env to verify the correctness of your lexical-address calculator:
    (define apply-env
      (lambda (env v depth position)
        (letrec ((apply-env-with-depth
                  (lambda (env current-depth)
                    (cases environment env
                      (empty-env-record () (eopl:error 'apply-env "No binding for ~s" sym))
                      (extended-env-record (syms vals env)
                                           (let ((pos (list-find-position v syms)))
                                             (cond
                                               ((number? pos) (let ((value (list-ref vals pos)))
                                                                (begin
                                                                  (write "Just found the value ")
                                                                  (write value)
                                                                  (write " at depth ")
                                                                  (write current-depth)
                                                                  (write " = ")
                                                                  (write depth)
                                                                  (write " and position ")
                                                                  (write pos)
                                                                  (write " = ")
                                                                  (write position)
                                                                  (newline)
                                                                  value)))
                                               (else (apply-env-with-depth env (+ current-depth 1))))))))))
          (apply-env-with-depth env 0))))
    
  3. (20 points) [EOPL] Exercise 3.30 on p. 91.

Note on submitting: submit three separate self-contained files, one for each of the problems.
Return Home