Created
July 4, 2012 04:44
-
-
Save swannodette/3045400 to your computer and use it in GitHub Desktop.
animals.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn node [q] | |
{:question q :left nil :right nil}) | |
(defn yes [q] | |
(println q) | |
(let [line (.toLowerCase (read-line))] | |
(= (first line) \y))) | |
(defn raw-input [q] | |
(println q) | |
(read-line)) | |
(defn run | |
([] (run [(node "bird")])) | |
([k] | |
(if-not (yes "Are you thinking of an animal?") | |
(recur k) | |
(let [p 0 | |
p (loop [p p] | |
(if (get-in k [p :left]) | |
(if (yes (str (get-in k [p :question]) "? ")) | |
(recur (get-in k [p :right])) | |
(recur (get-in k [p :left]))) | |
p))] | |
(if (yes (str "Is it a " (get-in k [p :question]) "? ")) | |
(recur k) | |
(let [animal (raw-input "What is the animals name?") | |
question (raw-input | |
(str "What question would distinguish a " | |
animal " from a " (get-in k [p :question]) "? ")) | |
nk (conj (assoc-in k [p :question] question) | |
(node (get-in k [p :question])) | |
(node animal)) | |
[li ri] ((juxt #(- % 2) #(dec %)) (count nk)) | |
nk (if-not (yes (str "If the animal were " animal " the answer would be? ")) | |
(update-in nk [p] #(merge % {:left ri :right li})) | |
(update-in nk [p] #(merge % {:left li :right ri})))] | |
(recur nk))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment