我有这段代码。
(defn get-movie [name-movie contents]
(loop [n (count contents) contents contents]
(let [movie (first contents)]
(if (= (:name (first contents)) name-movie)
(movie)
(recur (dec n) (rest contents))))))我有一系列的映射({:id,:name,:price} {} {})。我需要找到我给出的带有:名称的地图(匹配的电影)。当我给予的时候
(get-movie "Interstellar" contents)其中内容是
({:id 10000 :name "Interstellar" :price 1}{:id 10001 :name "Ouija" :price 2}). 我得到了以下异常。:
电影:传入的参数数(0)错误: C:\Users\Shalima\Documents\Textbooks\Functional AFn.java:437 clojure.lang.AFn.throwArity AFn.java:35 clojure.lang.AFn.invoke C:\Users\Shalima\Documents\Textbooks\Functional Programming\Programs\Assignment5.clj:53 file.test/get-clojure.lang.ArityException clojure.lang.ArityException Programming\Programs\Assignment5.clj:77 file.test/eval6219
我已经对这个问题坐了一段时间了,但仍然不知道哪里出了问题。我在这里做错了什么?
发布于 2014-11-11 13:25:52
您正在调用电影(地图),就像调用函数一样。可以使用键调用映射以进行查找,但没有0-arity形式。您可能只想返回电影,而不是调用它(通过用括号将其括起来)。
(defn get-movie [name-movie contents]
(loop [n (count contents) contents contents]
(let [movie (first contents)]
(if (= (:name (first contents)) name-movie)
movie ;; don't invoke
(recur (dec n) (rest contents))))))这对这个问题并不重要,但是使用解构来编写这个循环的更简单的方法是:
(defn get-movie [name-movie contents]
(loop [[{n :name :as movie} & movies] contents]
(if (= n name-movie)
movie ;; don't invoke
(recur movies))))如果你想转移到更高阶的序列函数,并且完全远离低级循环,你可以这样做:
(defn get-movie [name-movie contents]
(first (filter #(= name-movie (:name %)) contents)))https://stackoverflow.com/questions/26857944
复制相似问题