在这段clojure代码中:
(defn makeStructs ;line 27
"open fName as a file and turns each line into a struct. Returns a Vector of structs"
[fName]
with-open[r (reader (file fName))]
(let [r
res (doall (map makeStruct (line-seq r)))
]
(. r close)
res
)
)
我收到此编译器错误:
Exception in thread "main" java.lang.Exception: Can't take value of a macro: #'clojure.core/with-open (clojureHW.clj:27)
第27行在上面评论过。
知道问题是什么吗?
答案 0 :(得分:8)
你需要实际调用宏
(defn makeStructs ;line 27
"..."
[fName]
(with-open ; note the extra paren
答案 1 :(得分:7)
(defn makeStructs ;line 27
"open fName as a file and turns each line into a struct. Returns a Vector of structs"
[fName]
with-open[r (reader (file fName))]
(let [r
res (doall (map makeStruct (line-seq r)))
]
(. r close)
res
)
)
这不能正常工作,因为
with-open
周围没有任何问题。这将是一个正常的符号,它不会被称为。您的let中的表单数量不均匀。你有r,res aund(doall ......)。你已经在with-open中为'r'做了正确的绑定。你所需要的只是
(让[res(doall(map makeStruct(line-seq r)))] ....)
为什么你会(. r close)
使用open-open宏来实现这一点:http://clojuredocs.org/clojure_core/clojure.core/with-open
所以你得到:
(defn makeStructs
"open fName as a file and turns each line into a struct. Returns a Vector of structs"
[fName]
(with-open [r (reader (file fName))]
(let [res (doall (map makeStruct (line-seq r)))]
res)))
但是因为你只有一件事让你真的不需要:
(defn makeStructs
"open fName as a file and turns each line into a struct. Returns a Vector of structs"
[fName]
(with-open [r (reader (file fName))]
(doall (map makeStruct (line-seq r)))))
Lisp语言非常简单,大多数程序员只是想让自己变得困难,因为他们已经习惯了。你遇到的很多问题都是因为你已经习惯了像X这样的工作,但现在他们像Y一样工作。尽量不要认为事情就像X一样,你会让你的生活更轻松。
答案 2 :(得分:1)
要在看到此错误时添加到调试工具包中,我建议您查找正在定义或调用的函数,而不使用左括号'('。
在您的特定情况下,正如其他答案已经注意到的那样,您的代码缺少'('at with-open。
(defn makeStructs ;line 27
"open fName as a file and turns each line into a struct.
Returns a Vector of structs"
[fName]
--> with-open[r (reader (file fName))]
你没有打电话给我,而是取其价值。
我的错误如下:
--> defn ret-col-match
"Returns true false match on sequence position w/ cmp-val."
[row cmp-val col-idx]
(if (= (nth row col-idx nil) cmp-val)
true
false))
你可以看到缺少的'('在defn之前。