Skip to content

Instantly share code, notes, and snippets.

Created Feb 7, 2019
What would you like to do?
My first clojure program!
(ns user)
(defn askForNumber
"Asks for a number within a range, returning nil if input is not a number."
[ low high ]
(print (str "Pick a number from " low " to " high ": "))
(let [ num (read-string (read-line))]
(if (number? num) num nil))
(defn guess
"Play the game with a given range and secret number."
[low high answer]
(let [num (askForNumber low high)]
(case num
nil (do
(println "That's not a number!")
(guess low high answer))
(= num answer) (println (str "You guessed it: the number was " answer "!"))
(< num answer) (do
(println "Too low!")
(guess low high answer)
(> num answer) (do
(println "Too high!")
(guess low high answer)
(defn main
"Guess the secret number!"
(let [ low 1
high 100
answer (+ low (rand-int (- high low)))]
(guess low high answer)))
; Call (main) in the repl to play!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment