Scheme忽略对read-line的调用

时间:2014-08-06 04:36:36

标签: io scheme racket

当我运行我的方案程序时,它不会调用读取程序来等待标准输入的输入。相反,它似乎忽略了调用并返回一个空字符串。错误来自的代码是

(define (turn probes)
  (let ((guess (get-guess probes)))
    (display guess)
    (let ((curr_scr (get-score)))
      (reduce-pool probes guess (score-to-str curr_scr)))))

(define (get-score)
  (newline)
  (display "> ")
  (regexp-split #px":" (read-line)))

因此,当我调用get-score时,返回值为“”而不是标准输入。从标准读取后的预期返回类似于(1 2),并且第一个元素最终传递给make-string,从而导致以下错误:

make-string: expects type <non-negative exact integer> as 1st argument, given: ""; other arguments were: #\B

这是我第一次在计划中编程,所以我很困惑为什么会这样。我在Ubuntu 12.04上用mzscheme运行它,我相信它只是Racket的旧名称。对于发生了什么的任何想法?您需要更多信息吗?

2 个答案:

答案 0 :(得分:1)

这似乎与您之前使用的(read)有关。在代码的底部,替换两个

(define ... (read))

(define ... (string->number (read-line)))

答案 1 :(得分:1)

您是否在Racket REPL中运行程序?如果是这样,第一个(read-line)调用总是在第一次调用时返回一个空字符串。您可以通过在REPL中运行(list (read-line) (read-line))来复制它。

阅读此主题以获取更多详细信息:http://lists.racket-lang.org/users/archive/2011-January/043731.html