一日空いてしまったので復習から。
問題解答
問2.68
(define (encode-symbol symbol tree) (define (it symbol tree code) (cond ((null? symbol) '()) ((leaf? tree) code) ((element-of-set? symbol (symbols (left-branch tree))) (it symbol (left-branch tree) (append code '(0)))) ((element-of-set? symbol (symbols (right-branch tree))) (it symbol (right-branch tree) (append code '(1)))) (else (error "bad symbol" symbol)))) (it symbol tree '())) (define (encode message tree) (if (null? message) '() (append (encode-symbol (car message) tree) (encode (cdr message) tree)))) (encode '(A D A B B C A) sample-tree)
答えはでたけど何かコードが汚い。