Blob


1 (load "common")
2 (defun make-leaf (symbol weight)
3 (list 'leaf sym weight))
4 (defun leaf? (obj)
5 (eq (car obj) 'leaf))
6 (defun symbol-leaf (x)
7 (cadr x))
8 (defun weight-leaf (x)
9 (caddr x))
10 (defun make-code-tree (left right)
11 (list left
12 right
13 (append (symbols left) (symbols right))
14 (+ (weight left) (weight right))))
15 (defun left-branch (tree)
16 (car tree))
17 (defun right-branch (tree)
18 (cadr tree))
19 (defun symbols (tree)
20 (if (leaf? tree)
21 (list (symbol-leaf tree))
22 (caddr tree)))
23 (defun weight (tree)
24 (if (leaf? tree)
25 (weight-leaf tree)
26 (cadddr tree)))
27 (defun adjoin-set (x set)
28 "Add a new element _x_ into a set of elements, sorted by weight"
29 (cond ((null set) (list x))
30 ((< (weight x) (weight (car set)))
31 (cons x set))
32 (t (cons (car set)
33 (adjoin-set x (cdr set))))))