【问题标题】:Scheme definition error(short)方案定义错误(短)
【发布时间】:2015-04-02 20:12:06
【问题描述】:

这是我正在制作的解释器的一部分。我不断收到此错误:

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)

【问题讨论】:

  • 您到底为什么要使用 371 版?那是快五岁了!最新版本是 6.1.1,现在称为Racket

标签: scheme racket r5rs


【解决方案1】:

在您的解释器中,似乎定义只能出现在函数的开头。您应该改用let*

(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,那么到底如何修改ret1?我建议你读一本关于 Scheme 编程的好书,然后开始以更实用的风格编写代码,目前你的代码不是惯用的,你迟早会遇到麻烦。

【讨论】:

  • 啊好的,我这里有一个类似的问题(define repl( lambda() (display "\nUofL>") (define inp (read-command)))(define lengtha (length com)跨度>
  • 刚刚做了,你能帮忙编辑一下这个例子吗?
  • 解决方法是一样的:在函数内部停止使用define,而是在开头使用let*来声明局部变量。
  • 它给了我错误; let*: 语法错误(不是标识符--表达式绑定序列) in: inp
  • @zero6142 好的,我编辑了我的答案。但说真的,你必须阅读一本好书或教程,这是基本的Scheme。
猜你喜欢
  • 1970-01-01
  • 2014-05-30
  • 2013-03-13
  • 2012-10-25
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
相关资源
最近更新 更多