【发布时间】:2016-01-20 05:43:12
【问题描述】:
我是 clojure 新手,一直在关注 http://www.braveclojure.com 上的精彩教程。
我正在尝试编写一个新的地图函数,该函数将返回一组结果。
到目前为止,我有以下代码,但我不断收到一条错误消息:
java.lang.IllegalArgumentException: Parameter declaration "mapset" should be a vector
这是代码(我使用的是 Leiningen):
(defn mapset
"Works like map but returns a set"
; First call to the function
([f items]
(mapset f items []))
; Other calls to the function
([f items result]
(if (empty? items)
(set result)
(recur f (rest items) (conj result (f (first items)))))))
(defn -main
"Call the mapset function"
(mapset inc [1 1 2 2]))
我的第一个版本实际上使用了let,如下所示,但抛出了同样的错误:
(defn mapset
"Works like map but returns a set"
; First call to the function
([f items]
(mapset f items []))
; Second call to the function
([f items result]
(if (empty? items)
(set result)
(let [[item & other-items] items]
(recur f (into [] other-items) (conj result (f item)))))))
非常感谢任何帮助(以及对代码的批评)。
谢谢!
【问题讨论】:
标签: clojure