Clojure(读取行)返回nil;没有提示

时间:2011-05-02 19:13:28

标签: clojure

我正在研究我的第一个适当的Clojure计划 - 国际象棋游戏。我有以下内容:

(defn human-move [board]
  (board-utils/print-board board)
  (print "Enter your move, like this: 'E7 E5' ...")
  (loop [raw-move (terminal-input)] ;;(read-line)]
    (println "I just received" raw-move)
    (if (re-matches #"[A-H][1-8]\s[A-H][1-8]" raw-move)
      (parse-move raw-move)
      (do
        (println "Invalid format! There should be a letter, number, space, letter, and final number.")
        (print "Try again: ")
        (recur (read-line))))))

请注意read-line被注释掉并替换为terminal-input的位置。 read-line给了我一个NullPointerException,因此出于诊断目的:

(defn terminal-input []
  (println "input")
  (let [whatnot (read-line)]
    (println "received" whatnot)
    whatnot))

然后,当我拨打human-move时。

...
+---+---+---+---+---+---+---+---+
| P | P | P | P | P | P | P | P |
+---+---+---+---+---+---+---+---+
| R | N | B | Q | K | B | N | R |
+---+---+---+---+---+---+---+---+
Enter your move, like this: 'E7 E5' ...input
received nil
I just received nil

我从未有机会输入内容作为输入。如果是这个Java,我会开始用垃圾收集器玩小游戏(例如调用Scanner.next()),但是使用Clojure我除了将(flush)放在那里之外我不知道该怎么办。 / p>

对于它的价值,这是SLIME。


我还尝试让terminal-input提供虚拟数据,并了解到我显然错误地使用loop / recur。我没有彻底调查它,因为我被read-line问题分散了注意力。

提前致谢。

3 个答案:

答案 0 :(得分:8)

read-line在SLIME中不起作用。我无法找到关于此的讨论主题,但这是真的。

答案 1 :(得分:8)

现在可以使用swank-clojure 1.4.0-SNAPSHOT,如果你在swank.core / with-read-line-support中调用read-line,就像这样

(with-read-line-support (println "a line from Emacs:" (read-line))

https://github.com/technomancy/swank-clojure/commit/f4a1eebc4d34f2ff473c4e5350f889ec356f5168

答案 2 :(得分:0)

最后这对我有用:

(swank.core/with-read-line-support
   (read-line))