Robot Mouse in Maze
Depth-first search of an implicit tree can simulate a robot mouse in a maze. The goal is to return a sequence of steps to guide the mouse to the cheese.
(defn mouse [maze x y prev]
(if (or (= (nth (nth maze y) x) '*) ; hit wall
(member (list x y) prev)) ; been there
nil ; fail
(if (= (nth (nth maze y) x) 'c) ; cheese
'() ; success
(let [path (mouse maze (- x 1) y ; go west
(cons (list x y) prev))]
(if path (cons 'w path)
(let [path (mouse maze x (- y 1)
(cons (list x y) prev))]
(if path (cons 'n path)
(let [path (mouse maze (+ x 1) y
(cons (list x y) prev))]
(if path (cons 'e path)
(let [path (mouse maze x (+ y 1)
(cons (list x y) prev))]
(if path (cons 's path)
nil))))))))))) ; fail