首页 文章

将“简单方案”语言添加到DrRacket

提问于
浏览
2

我想通读这本书:http://www.eecs.berkeley.edu/~bh/ss-toc2.html . 但我跑了.1832347_t .

#lang planet dyoo/simply-scheme:2
    (parse ’(4 + 3 * 7 - 5 / (3 + 4) + 6))

我不断收到以下错误消息:“解析:模块中的未绑定标识符:解析” .

3 回答

  • 2

    看看这个page,它有完整的说明 . 只需这样做:

    #lang racket
    (require (planet dyoo/simply-scheme:2:2))
    

    另请注意, 字符不正确,引用 ' ,这可能是因为您使用错误的排版复制粘贴的代码 .

    当然,完成上述操作后,您必须定义第18章中介绍的过程,它们未在您刚刚导入的包中定义!这肯定会起作用:

    (define (parse expr)
      (parse-helper expr '() '()))
    
    (define (parse-helper expr operators operands)
      (cond ((null? expr)
         (if (null? operators)
             (car operands)
             (handle-op '() operators operands)))
        ((number? (car expr))
         (parse-helper (cdr expr)
                   operators
                   (cons (make-node (car expr) '()) operands)))
        ((list? (car expr))
         (parse-helper (cdr expr)
                   operators
                   (cons (parse (car expr)) operands)))
        (else (if (or (null? operators)
                  (> (precedence (car expr))
                 (precedence (car operators))))
              (parse-helper (cdr expr)
                    (cons (car expr) operators)
                    operands)
              (handle-op expr operators operands)))))
    
    (define (handle-op expr operators operands)
      (parse-helper expr
            (cdr operators)
            (cons (make-node (car operators)
                     (list (cadr operands) (car operands)))
                  (cddr operands))))
    
    (define (precedence oper)
      (if (member? oper '(+ -)) 1 2))
    
    (define (compute tree)
      (if (number? (datum tree))
          (datum tree)
          ((function-named-by (datum tree))
             (compute (car (children tree)))
             (compute (cadr (children tree))))))
    
    (define (function-named-by oper)
      (cond ((equal? oper '+) +)
        ((equal? oper '-) -)
        ((equal? oper '*) *)
        ((equal? oper '/) /)
        (else (error "no such operator as" oper))))
    
    (parse '(4 + 3 * 7 - 5 / (3 + 4) + 6))
    => '(+ (- (+ (4) (* (3) (7))) (/ (5) (+ (3) (4)))) (6))
    
    (compute (parse '(4 + 3 * 7 - 5 / (3 + 4) + 6)))
    => 30 2/7
    
  • 0

    还有一个模块允许您在此处运行"Simply Scheme":https://gist.github.com/alexgian/5b351f367169b40a4ad809f0bb718e1f

    您可以运行它,然后以交互方式键入代码,无需像上面提到的Planet / dyoo代码那样安装 . 但是,如果您希望安装它,那么您所要做的就是使用 #lang simply-scheme 为任何程序添加前缀,它将起作用 . 有关如何操作的说明可以在这里看到:https://groups.google.com/forum/#!topic/racket-users/jHPtw3Gzqk4在Matthew Butterick的信息中,2018年6月4日 .

    例如,这个版本会处理一些没有完成dyoo代码的东西

    > (first 'american)
    'a
    > (first 'American)
    "A"
    > (every butfirst '(AmericAn Legacy CODE))
    '("mericAn" egacy "ODE")
    

    即不正确地混合字符串和符号,而不是

    > (first 'american)
    a
    > (first 'American)
    A
    > (every butfirst '(AmericAn Legacy CODE))
    (mericAn egacy ODE)
    

    应该如此 . 但是,对于文档等,dyoo实现可能更完整

    但是,如上面的答案中所述,您仍然必须自己输入解析函数的代码,因为这是作者的意图 .

  • 1

    我不得不恢复到DrRacket 5.4.1,以便让Simply Scheme和SICP工作 .

相关问题