DrScheme 与 mzscheme:定义的处理

发布于 2024-07-10 23:26:25 字数 2088 浏览 7 评论 0原文

我的一个长期项目是完成 SICP 的所有练习。 我注意到最近的练习有些奇怪。 我正在测试霍夫曼编码树。 当我在 DrScheme 中执行以下代码时,我得到了预期的结果:

(a d a b b c a)

但是,如果我通过调用 (load "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))

One long term project I have is working through all the exercises of SICP. I noticed something a bit odd with the most recent exercise. I am testing a Huffman encoding tree. When I execute the following code in DrScheme I get the expected result:

(a d a b b c a)

However, if I execute this same code in mzscheme by calling (load "2.67.scm") or by running mzscheme -f 2.67.scm, it reports:

symbols: expected symbols as arguments, given: (leaf D 1) 

My question is: why? Is it because mzscheme and drscheme use different rules for loading program definitions? The program code is below.

;; 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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-07-17 23:26:25

默认情况下,MzScheme 以一种存在符号定义的模式启动,并且它内联它所知道的函数——因此当它编译您的 make-code-tree 时> 定义,它使用它知道的绑定。 当它稍后编译您的符号时,它不会对之前的定义产生影响。

处理此问题的最简单方法是将代码放入模块中,并在其前面添加 #lang 方案

By default, MzScheme starts in a mode where there is an existing definition for symbols, and it inlines functions that it knows about -- so when it compiles your make-code-tree definition, it uses the binding it knows about. When it later compiles your symbols, it doesn't have an effect on the previous definition.

The easiest way to deal with this is to make your code into a module, by prefixing it with a #lang scheme.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文