How to get user input in Clojure?

JoOb picture JoOb · Jul 11, 2009 · Viewed 21.6k times · Source

I'm currently learning clojure, but I was wondering how to get and store user input in a clojure program. I was looking at the clojure api and I found a function called read-line, however I'm not sure how to use it if it's the right function to use...

Anyhow, how do you get user input in clojure ?

Answer

dbr picture dbr · Jul 11, 2009

read-line is the correct function..

(println (read-line))

..would basically echo the users input:

Clojure 1.0.0-
user=> (println (read-line))
this is my input
this is my input

To use it in an if statement, you'd probably use let:

(let [yayinput (read-line)]
  (if (= yayinput "1234")
    (println "Correct")
    (println "Wrong")))

Hope that's enough to get you started, because that's about the limit of my Clojure knowledge!