我的问题是关于Clojures deftest宏或者更普遍的关于如何比较函数创建的列表 . 但我是Clojure的新手,无法识别具体原因 . 也许其他人有想法?
首先报告的消息:
FAIL in(to-symbol-list-test)(util_test.clj:105)预期:(=(quote(a(not b)c))(to-symbol-list [“a”“(not b)” “c”]))实际:(不是(=(a(非b)c)(a(非b)c)))
但显然(=(a(非b)c)(a(非b)c))引用应该是真的 .
二,具体测试代码:
(deftest to-symbol-list-test
(is (= '(a (not b) c) (to-symbol-list ["a" "(not b)" "c"]))))
第三,to-symbol-list的定义:
(defn to-symbol-list [term]
"Converts a vector based term into a list based term
by converting its elements into symbols recursivly"
(postwalk
#(if (string? %)
(symbol %)
(reverse (into '() %)))
term))
该函数甚至应该转换嵌套向量 . 这是一个例子,其他功能的行为方式相同 . 我猜这可能是由不同类型引起的 . 例如list vs lazy-seq和i比较懒惰函数而不是数据,但类型似乎是正确的 . 在REPL我得到:
(type (to-symbol-list ["a" "(not b)" "c"]))
=> clojure.lang.PersistentList
2 回答
似乎
to-symbol-list
将"(not b)"
字符串变为符号,但不是嵌套列表 . 要解决这个问题,您需要重构该函数以将parens考虑在内 . 比方说,如果它将(
视为字符串的第一个符号,它会自动递归地将结果附加到某种累加器中(顺便提一下,SICP充满了这样的练习) .to-symbol-list
返回一个包含3个符号的列表,并且没有预料到't recursively deal with the nested data structure. Unfortunately the second symbol prints the same as the correctly parsed data structure you' . 我认为在这种情况下你最好使用clojure.edn/read-string
(docs here)来解析你认为你期待的数据结构 .此外,作为将来帮助诊断此类事物的提示,您可以将一个额外的参数传递给
clojure.test/is
,该参数将在发生故障时打印出来 . 这可能是函数调用的结果,如: