DrScheme与使用MzScheme:治疗的定义
题
一个长期的项目,我也正在努力通过SICP的所有练习。我注意到的东西有点古怪与最近的演习。我测试霍夫曼编码树。当我在DrScheme执行下面的代码我得到预期的结果:
(a d a b b c a)
然而,如果我通过调用(负载 “2.67.scm”)或通过运行执行在此的MzScheme相同的代码的MzScheme -f 2.67.scm,它报告:
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))
解决方案
默认情况下,才能使用MzScheme开始在那里是symbols
现有定义一个模式,它内联它所知道的功能 - 所以当它编译你的make-code-tree
定义,它使用它知道的结合。当后来编译你的symbols
,它不会对以前的定义产生影响。
要解决这个问题是让你的代码到一个模块中,通过用#lang scheme
前缀它最简单的方法。
不隶属于 StackOverflow