Skip to content

Instantly share code, notes, and snippets.

@ryan-haskell
Created February 7, 2019 23:01
Show Gist options
  • Save ryan-haskell/34e15c2f87367fdfcd60a2a17beedcef to your computer and use it in GitHub Desktop.
Save ryan-haskell/34e15c2f87367fdfcd60a2a17beedcef to your computer and use it in GitHub Desktop.
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 ]
(do
(print (str "Pick a number from " low " to " high ": "))
(flush)
(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))
(cond
(= 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