本文介绍了添加“简单方案"DrRacket 的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完成这本书:http://www.eecs.berkeley.edu/~bh/ss-toc2.html.但是我很难让Simply Scheme"语言工作.代码将无法运行.

I want to work through this book: http://www.eecs.berkeley.edu/~bh/ss-toc2.html. But I'm having trouble to get the "Simply Scheme" language working. The code won't run.

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

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

I keep getting the following error message: "parse: unbound identifier in module in: parse".

推荐答案

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

Take a look at this page, it has complete instructions. Simply do this:

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

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

Also be aware that the character is incorrect, for quoting use ', this probably happened because you copy-pasted code with wrong typesetting.

当然,在上面完成之后,你必须定义第18章中解释的过程,它们不是在你刚刚导入的包中定义的!这肯定会起作用:

And of course, after the above is done, you have to define the procedures explained in chapter 18, they're not defined in the package you just imported! this will work for sure:

(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

这篇关于添加“简单方案"DrRacket 的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:02