本文介绍了使用 Racket 实现 SICP 评估器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 4.1.4 Running the Evaluator as a Program 的元循环计算器,使用 Racket 构建它:

I'm working on metacircular evaluator of 4.1.4 Running the Evaluator as a Program, building which with Racket:

#lang racket

(require (combine-in rnrs/base-6
                     rnrs/mutable-pairs-6))

(define (evaluate exp)
  (cond 
    ; ...
    ((definition? exp) (display exp)
                       (display " is a definition\n"))
    ; ...
    (else (display exp) 
          (display " is something else\n"))))

(define (definition? exp)
  (tagged-list? exp 'define))

(define (tagged-list? exp tag)
  (if (pair? exp)
      (eq? (car exp) tag)
      false))

(define (driver-loop)
  (let ((input (read)))
    (let ((output (evaluate input)))
      output))
  (driver-loop))

(driver-loop)

在成功读取DrRacket中的输入框后,我输入(define a 0),结果是:

After getting a box that reads input in DrRacket successfully, I type in (define a 0) and it turn out:

(定义一个 0)是别的东西

如果我删除它可以被识别

It could be recognised if I remove

(require (combine-in rnrs/base-6
                     rnrs/mutable-pairs-6))

但没有它我将无法调用 set-car!set-cdr!.set- 函数有替代方法吗?

But without which I wouldn't be able to call set-car! or set-cdr!. Is there an alternative for set- function?

或者我可以选择从 rnrs/base-6rnrs/mutable-pairs-6 导入什么?

Or could I choose what to import from rnrs/base-6 and rnrs/mutable-pairs-6 ?

推荐答案

这里是错误:

(require (combine-in rnrs/base-6
                     rnrs/mutable-pairs-6))

rnrs/base-6rnrs/mutable-pairs-6 带来了一些不可预测的变化 cons(以及 car, cdr) 前导 (define a 0) 没有被 definition 捕获?

Package rnrs/base-6 and rnrs/mutable-pairs-6 bring in something unpredicted that change cons(as well as car, cdr) leading (define a 0) not been caught by definition?

解决方案:

(require (only-in (combine-in rnrs/base-6
                              rnrs/mutable-pairs-6)
                  set-car!
                  set-cdr!))

始终将 only-in 放在 require 中以避免任何不需要的绑定.

Always put only-in in require to avoid any unwanted binding.

这篇关于使用 Racket 实现 SICP 评估器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 10:20