-1

这是我正在制作的口译员的一部分。我不断收到此错误:

define not allowed in an expression context in: (define ret1 (list->string wl))

我正在使用 DrScheme 版本 371,语言标准 (R5RS)。

(define (read-command)
  (set! com '( '() ))
  (set! wl (read-as-list))
  (define ret1 (list->string wl))
  (commRead)
  ret1
 )

类似的问题在这里:

    (define repl(
                 lambda()
                  (display "\nUofL>")
                  (define inp (read-command))
                  (define lengtha (length com)

4

1 回答 1

1

在您的解释器中,似乎定义只能出现在函数的开头。您应该使用 alet*代替:

(define (read-command)
  (let* ((com '('())) ; are you sure you didn't mean '(()) ?
         (wl (read-as-list))
         (ret1 (list->string wl)))
  (commRead ret1)))

对于第二个问题,试试这个:

(define repl
  (lambda ()
    (display "\nUofL>")
    (let ((inp (read-command))
          (lengtha (length com)))
      ; return a value here
      )))

附带说明一下,您的代码似乎是用程序风格编写的——所有这些set!和函数调用都是为了效果而执行的。ret1如果您不将其作为参数传递给,究竟将如何进行修改commRead?我建议你阅读一本关于 Scheme 编程的好书,然后开始以更实用的风格编写代码,目前你的代码不是惯用的,你迟早会遇到麻烦。

于 2015-04-02T20:21:13.963 回答