CS212 Exams
Spring 1999 - Prelim 2

Solution to OO Programming III

WARNING: You should seriously try to answer this question before looking at the solution -- it's just a good study skill. Also, some answers on this page may be more brief than you would want to put down in an actual exam. If you want any type of partial credit, you should write more than just the solution.


(define (has-path? x y)
  (define (path? to-check history)
    (cond ((empty? to-check) #f)
          ((eq? (head to-check) y) #t)
          ((document? (head to-check))
           (path? (tail to-check)
                  (cons (head to-check) history)))
          ((symbolic-link? (head to-check))
           (let ((current (head to-check)))
             (path? (if (memq (object current) history)
                      (tail to-check)
                      (cons (object current) (tail to-check)))
                    (cons current history))))
          (else                  ;;; this is the case for directory
           (let ((current (head to-check))
                 (next-up (filter (lambda (x) (not (memq x history)))
                                  (contents current))))
             (path? (append (tail to-check) next-up)
                    (cons current history))))))
  (path? (list x) empty))

Question

Return to CS 212 Prelim 2 - Spring 1999