Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created July 19, 2012 20:17
Show Gist options
  • Save noahlz/3146478 to your computer and use it in GitHub Desktop.
Save noahlz/3146478 to your computer and use it in GitHub Desktop.
;; Robot from Joy of Clojure, Ch 7
(def bearings [{:x 0 :y 1} ; move north
{:x 1 :y 0} ; move east
{:x 0 :y -1} ; move south
{:x -1 :y 0}]) ; move west
(defn bot [x y bearing-num]
{:coords [x y]
:bearing ([:north :east :south :west] bearing-num)
:forward (fn [] (bot (+ x (:x (bearings bearing-num)))
(+ y (:y (bearings bearing-num)))
bearing-num))
:turn-right (fn [] (bot x y (mod (+ bearing-num 1) 4))) ;; Note this is different from the book
:turn-left (fn [] (bot x y (mod (- bearing-num 1) 4)))}) ;; It had (mod (<op> 1 bearing-num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment