Reputation: 55
I made a function to take questions like this.
(defn ask-ques [ques pred]
(print ques)
(let [user-input (read-line)]
(if #(pred user-input) user-input (recur ques pred))))
And I wrote main like this.
(defn -main []
(loop []
(let [user-input (ask-ques "CHOOSE ONE. (C)ontinue OR (E)xit : " #(contains? #{"C" "E"} %))]
(when (= user-input "C") (apply body (rand-nth (seq voc-map))) (recur)))))
But, Clojure received the input first and printed "CHOOSE ONE. (C)ontinue OR (E)xit : " out, and pred does not work well.
What's the problem? Why does it work like this? And what should I do?
Upvotes: 1
Views: 104
Reputation: 6666
#(pred user-input)
is a function of zero arguments and, since it has a non-nil
value, the if
will treat it as truth, so you will always get user-input
and it will never recur
. I suspect you want (pred user-input)
instead.
Upvotes: 2