Running SICP Pattern Matching Rule Based Substitution Code

烂漫一生 提交于 2019-11-30 05:16:34
soegaard

Your code is from 1991. Since R5RS came out in 1998, the code must be written for R4RS (or older). One of the differences between R4RS and later Schemes is that the empty list was interpreted as false in the R4RS and as true in R5RS.

Example:

  (if '() 1 2)

gives 1 in R5RS but 2 in R4RS.

Procedures such as assq could therefore return '() instead of false. This is why you need to change the definition of extend-directory to:

(define (extend-dictionary pat dat dictionary)
  (let ((vname (variable-name pat)))
    (let ((v (assq vname dictionary)))
      (cond ((not v)
             (cons (list vname dat) dictionary))
            ((eq? (cadr v) dat) dictionary)
            (else 'failed)))))

Also back in those days map was called mapcar. Simply replace mapcar with map.

The error you saw in DrRacket was:

mcdr: expects argument of type <mutable-pair>; given '()

This means that cdr got an empty list. Since an empty list has no cdr this gives an error message. Now DrRacket writes mcdr instead of cdr, but ignore that for now.

Best advice: Go through one function at a time and test it with a few expressions in the REPL. This is easier than figuring everything out at once.

Finally begin your program with:

(define user-initial-environment (scheme-report-environment 5))

Another change from R4RS (or MIT Scheme in 1991?).

Addendum:

This code http://pages.cs.brandeis.edu/~mairson/Courses/cs21b/sym-diff.scm almost runs. Prefix it in DrRacket with:

#lang r5rs
(define false #f)
(define user-initial-environment (scheme-report-environment 5))
(define mapcar map)

And in extend-directory change the (null? v) to (not v). That at least works for simple expressions.

Here is the code that works for me with mit-scheme (Release 9.1.1).

You also may use this code. It runs on Racket.

For running "eval" without errors, the following needed to be added

(define ns (make-base-namespace))
(apply (eval '+ ns) '(1 2 3))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!