3
我有一個長期項目正在通過SICP的所有練習。我注意到最近的練習有點奇怪。我正在測試霍夫曼編碼樹。當我在DrScheme執行下面的代碼我得到預期的結果:DrScheme與mzscheme:治療定義
(a d a b b c a)
但是,如果我通過調用(載「2.67.scm」)或者通過運行的MzScheme -f 2.67.scm執行中的MzScheme相同的代碼它報告:
symbols: expected symbols as arguments, given: (leaf D 1)
我的問題是:爲什麼?是否因爲mzscheme和drscheme使用不同的規則來加載程序定義?程序代碼如下。
;; Define an encoding tree and a sample message
;; Use the decode procedure to decode the message, and give the result.
(define (make-leaf symbol weight)
(list 'leaf symbol weight))
(define (leaf? object)
(eq? (car object) 'leaf))
(define (symbol-leaf x) (cadr x))
(define (weight-leaf x) (caddr x))
(define (make-code-tree left right)
(list left
right
(append (symbols left) (symbols right))
(+ (weight left) (weight right))))
(define (left-branch tree) (car tree))
(define (right-branch tree) (cadr tree))
(define (symbols tree)
(if (leaf? tree)
(list (symbol-leaf tree))
(caddr tree)))
(define (weight tree)
(if (leaf? tree)
(weight-leaf tree)
(cadddr tree)))
(define (decode bits tree)
(define (decode-1 bits current-branch)
(if (null? bits)
'()
(let ((next-branch
(choose-branch (car bits) current-branch)))
(if (leaf? next-branch)
(cons (symbol-leaf next-branch)
(decode-1 (cdr bits) tree))
(decode-1 (cdr bits) next-branch)))))
(decode-1 bits tree))
(define (choose-branch bit branch)
(cond ((= bit 0) (left-branch branch))
((= bit 1) (right-branch branch))
(else (error "bad bit -- CHOOSE-BRANCH" bit))))
(define (test s-exp)
(display s-exp)
(newline))
(define sample-tree
(make-code-tree (make-leaf 'A 4)
(make-code-tree
(make-leaf 'B 2)
(make-code-tree (make-leaf 'D 1)
(make-leaf 'C 1)))))
(define sample-message '(0 1 1 0 0 1 0 1 0 1 1 1 0))
(test (decode sample-message sample-tree))