Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created July 4, 2012 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/3045400 to your computer and use it in GitHub Desktop.
Save swannodette/3045400 to your computer and use it in GitHub Desktop.
animals.clj
(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