【问题标题】:What is wrong in this attempt at a clojure map rewrite这次尝试重写 clojure map 有什么问题
【发布时间】: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


    【解决方案1】:

    经过进一步调查,我意识到我忘了添加:

    [& args]
    

    -main函数中文档字符串的末尾,如:

    (defn -main
    "Call the mapset function"
    [& args]
    (mapset inc [1 1 2 2]))
    

    【讨论】:

      猜你喜欢
      • 2012-07-11
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2019-09-18
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      相关资源
      最近更新 更多