【问题标题】:Error with the following code以下代码出错
【发布时间】:2013-02-28 04:12:08
【问题描述】:

当我为这段代码做(访问医生晚餐时间)时:

(define (visit-doctor name)
  (if (equal? name 'suppertime) (end-session)
  ((write-line (list 'hello name))
  (write-line '(what seems to be the trouble?))
  (doctor-driver-loop name initial-earlier-response))))

(define (end-session) (write-line '(the doctor is done seeing patients today)))

它给了我这个错误:

应用程序:不是程序; 期望一个可以应用于参数的过程 给定:# 论据...: # #

【问题讨论】:

  • 如果您使用 DrRacket,您是否看到与 Racket 认为错误可能所在的位置相对应的红色突出显示?我在((write-line (list 'hello name)) 线上看到了一个问题:对我来说,它看起来像是一个双功能应用程序,您可能打算在其中执行一个应用程序。对于这种特定情况,DrRacket 应该为您提供此错误的精确位置。你看到了吗?

标签: scheme racket


【解决方案1】:

您的问题是您尝试使用方括号对代码块进行分组。
Scheme 不这样做。

您的else 分支是

((write-line (list 'hello name))
 (write-line '(what seems to be the trouble?))
 (doctor-driver-loop name initial-earlier-response))

这是一个包含三个元素的列表。

该列表的第一个元素应用于过程,然后将其应用于其他两个元素,但是当您评估 (write-line (list 'hello name)) 时,您没有得到过程,而是得到 #<void>

解决方法是使用begin对其进行排序:

(begin (write-line (list 'hello name))
       (write-line '(what seems to be the trouble?))
       (doctor-driver-loop name initial-earlier-response))

【讨论】:

    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2021-08-21
    • 2018-01-02
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多