[英]What's expecting a function in this clojure code?
going through the exercises in the fp-oo book and i'm having trouble with an early exercise to add squares. 通过fp-oo书中的练习,我在早期练习添加方格上遇到了麻烦。 Here's my code: 这是我的代码:
(defn square [n]
(* n n))
(defn add-squares [l]
(cond
(nil? l) 0
:else (+ (square (first (l))) (add-squares (rest (l))))))
This example: 这个例子:
(add-squares '(2 2 2 2))
should return 应该回来
16
but fails with this exception: 但由于以下异常而失败:
ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn user/add-squares (NO_SOURCE_FILE:4)
which i guess means i'm trying to pass a function somewhere instead of a list which was expected. 我想这意味着我试图在某个地方传递一个函数,而不是期望的列表。 Can someone tell me which part of the code is the culprit? 有人可以告诉我是罪魁祸首的哪一部分吗?
Thanks, James 谢谢,詹姆斯
This is wrong: 这是错误的:
(first (l))
This means that you are calling l as a function. 这意味着您正在调用l作为函数。
You should use: 您应该使用:
(first l)
And of course, the same thing for (rest (l)) 当然,(休息(l))也是一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.