Code Review

Compare your solutions

    #| BEGIN (Write your solution here) |#
(define (encode-symbol symbol tree)
  (define (encode-symbol-impl symbol tree)
    (cond ((not (element-of-set? symbol (symbols tree))) #f)
          ((leaf? tree) '())
          (else
           (let ((left-try (encode-symbol-impl symbol (left-branch tree)))
                 (right-try (encode-symbol-impl symbol (right-branch tree))))
             (cond ((and (not (equal? left-try #f))
                         (not (equal? right-try #f)))
                    #f)
                   ((not (equal? left-try #f))
                    (cons 0 left-try))
                   ((not (equal? right-try #f))
                    (cons 1 right-try))
                   (else #f))))))
    (let ((result (encode-symbol-impl symbol tree)))
      (if (equal? result #f)
          (error "tree does not contain symbol or ambiguous" symbol)
          result)))

(define (element-of-set? x set)
  (cond ((null? set) #f)
        ((eq? x (car set)) #t)
        (else (element-of-set? x (cdr set)))))

(define (left-branch tree) (car tree))

(define (right-branch tree) (cadr tree))
#| END |#