在Clojure重写Lisp土地的向导游戏

Chi*_*ron 9 lisp jvm clojure land-of-lisp

我正试图从"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))))
Run Code Online (Sandbox Code Playgroud)

尝试时:

   (println (describe-paths :attic *edges*))
Run Code Online (Sandbox Code Playgroud)

我有这个例外:

线程"main"中的异常java.lang.RuntimeException:java.lang.IllegalArgumentException:不知道如何创建ISeq:clojure.lang.Symbol(wizard-game.clj:0)

我还没有一个Lispy的眼睛,我做错了什么?

Rob*_*lan 8

将它放入REPL并运行跟踪:

user> (ns foo (:use clojure.contrib.trace))
nil
Run Code Online (Sandbox Code Playgroud)

此时,我将您的代码复制到REPL中.(未显示)

接下来,我运行一个跟踪:

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> 
Run Code Online (Sandbox Code Playgroud)

所以问题是(describe-path-raw living-room).正如错误消息所指出的那样,起居室是一个符号,这个函数正在尝试做最后一个和第二个调用的事情,这只能在序列上完成.

那为什么会这样呢?

在describe-paths中,您正在调用(位置边).这里的位置是:阁楼,边缘是地图.因此,(位置边缘)可以到达(楼下的客厅梯子).您将describe-path-raw映射到此列表,该列表适用于:

((describe-path-raw living-room) (describe-path-raw downstairs) (describe-path-raw ladder))
Run Code Online (Sandbox Code Playgroud)

这是在第一次通话时抛出异常,因为起居室是一个符号,而不是一个序列.