gpt4 book ai didi

clojure - 用 Clojure 重写 Land of Lisp Wizard 游戏

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:30 25 4
gpt4 key购买 nike

我正在尝试重写“Land of Lisp”中的 Wizard 游戏 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))))

尝试时:

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

我遇到了这个异常:

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)

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

最佳答案

将其放入 REPL 并运行跟踪:

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

此时我将您的代码复制到 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>

所以问题是(describe-path-raw living-room)。正如错误消息所指出的,客厅是一个符号,这个函数试图在它上面做一些事情,比如调用 last 和 second,这只能在序列上完成。

那么为什么会这样呢?

在 describe-paths 内部,您正在调用(位置边缘)。在这里,location 是 :attic,edges 是一张 map 。因此,(位置边缘)计算到(客厅楼下梯子)。您正在将 describe-path-raw 映射到此列表,结果是:

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

这会在第一次调用时抛出异常,因为 living-room 是一个符号,而不是一个序列。

关于clojure - 用 Clojure 重写 Land of Lisp Wizard 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5363393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com