Search code examples
clojurejvmlispland-of-lisp

Rewriting Wizard game of Land of Lisp in Clojure


I'm trying to rewrite the Wizard game from "Land of Lisp" http://landoflisp.com/wizards_game.lisp

(def *nodes* {:living-room "you are in the living-room. a wizard is snoring loudly on the couch."
          :garden "you are in a beautiful garden. there is a well in front of you."
          :attic "you are in the attic. there is a giant welding torch in the corner."})

(def *edges* {:living-room '((garden west door) (attic upstairs ladder))
          :garden '(living-room east door)
          :attic '(living-room downstairs ladder)})

(defn describe-location [location nodes]
  (nodes location))

(defn describe-path-raw [edge]
  `(there is a ~(last edge) going ~(second edge) from here.))

(defn describe-path [edge]
   (map #(symbol (name %)) (describe-path-raw edge)))

(defn describe-paths [location edges]
   (apply concat (map describe-path-raw (location edges))))

When trying:

   (println (describe-paths :attic *edges*))

I got this exception:

Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol (wizard-game.clj:0)

I don't have a Lispy eye yet, what I'm doing wrong?


Solution

  • Putting this into a REPL, and running trace:

    user> (ns foo (:use clojure.contrib.trace))
    nil
    

    At this point I copy in your code into the REPL. (not shown)

    Next, I run a trace:

    foo> (dotrace [describe-location describe-path-raw describe-path describe-paths]
                  (describe-paths :attic *edges*))
    TRACE t1662: (describe-paths :attic {:living-room ((garden west door) (attic upstairs ladder)),     :garden (living-room east door), :attic (living-room downstairs ladder)})
    TRACE t1663: |    (describe-path-raw living-room)
    ; Evaluation aborted.
    foo> 
    

    So the problem is (describe-path-raw living-room). As the error message points out, living-room is a symbol, and this function is trying to do things like call last and second on it, which can only be done on sequences.

    So why is this happening?

    Inside of describe-paths, you are calling (location edges). Here, location is :attic, and edges is a map. Therefore, (location edges) works out to (living-room downstairs ladder). You are mapping describe-path-raw onto this list, which works out to:

    ((describe-path-raw living-room) (describe-path-raw downstairs) (describe-path-raw ladder))
    

    And this is throwing an exception on the first call, since living-room is a symbol, not a sequence.