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

tsm*_*tsm 5 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))))))
Run Code Online (Sandbox Code Playgroud)

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

(defn terminal-input []
  (println "input")
  (let [whatnot (read-line)]
    (println "received" whatnot)
    whatnot))
Run Code Online (Sandbox Code Playgroud)

然后,当我打电话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
Run Code Online (Sandbox Code Playgroud)

我从来没有机会输入任何内容作为输入.如果是这个Java,我会开始用垃圾收集器(Scanner.next()例如调用)玩小游戏,但是对于Clojure我除了把它(flush)放在那里之外我不知道该做什么.

对于它的价值,这是SLIME.


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

提前致谢.

ama*_*loy 8

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


Tav*_*udd 8

如果你在swank.core/with-read-line-support中包含对read-line的调用,这将适用于swank-clojure 1.4.0-SNAPSHOT

(with-read-line-support (println "a line from Emacs:" (read-line))
Run Code Online (Sandbox Code Playgroud)

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