Note that there are some explanatory texts on larger screens.

plurals
  1. POClojure's (read-line) returns nil; does not prompt
    text
    copied!<p>I'm working on my first proper Clojure program--a chess game. I have the following:</p> <pre><code>(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)))))) </code></pre> <p>Note the place where <code>read-line</code> is commented out and replaced by <code>terminal-input</code>. <code>read-line</code> was giving me a NullPointerException, so for diagnostic purposes:</p> <pre><code>(defn terminal-input [] (println "input") (let [whatnot (read-line)] (println "received" whatnot) whatnot)) </code></pre> <p>Then, when I call <code>human-move</code>.</p> <pre><code>... +---+---+---+---+---+---+---+---+ | 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 </code></pre> <p>I never got a chance to type something in as input. Were this Java, I'd start playing little games with the garbage collector (calling <code>Scanner.next()</code>, for instance), but with Clojure I didn't know what to do besides putting the <code>(flush)</code> in there.</p> <p>For what it's worth, this is with SLIME.</p> <hr> <p>I also tried to have <code>terminal-input</code> provide dummy data, and learned that I'm apparently using <code>loop</code>/<code>recur</code> incorrectly. I haven't investigated it super-thoroughly though, since I've been distracted by the <code>read-line</code> issues. <code></code></p> <p><code></code></p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload