Running SICP Pattern Matching Rule Based Substitution Code

后端 未结 3 1991
广开言路
广开言路 2020-12-29 09:21

I have found the code from this lesson online (http://groups.csail.mit.edu/mac/ftpdir/6.001-fall91/ps4/matcher-from-lecture.scm), and I am having a heck of a time trying to

相关标签:
3条回答
  • 2020-12-29 10:03

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

    0 讨论(0)
  • 2020-12-29 10:03

    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))
    
    0 讨论(0)
  • 2020-12-29 10:05

    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.

    0 讨论(0)
提交回复
热议问题