SICP 2.3 exercises cont. 3

2.67

(decode sample-message sample-tree)
(A D A B B C A)

2.68

(define (encode-symbol symbol tree)
  (if (leaf? tree)
      (if (eq? (symbol-leaf tree) symbol)
          '()
          #f)
    (let ((left-result (encode-symbol symbol (left-branch tree))))
      (if left-result
          (cons 0 left-result)
        (let ((right-result (encode-symbol symbol (right-branch tree))))
          (if right-result
              (cons 1 right-result)
            (error "No such symbol in tree:" symbol)))))))

(encode '(A D A B B C A) sample-tree)
(0 1 1 0 0 1 0 1 0 1 1 1 0)

Snail’s pace now due to school. Oh well, as long as it’s consistent.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s