我是Clojure的新手,建议的挑战之一是用户实现Filter函数。
所以我想到了这个
(defn filterr
"Filter implementation"
[condition coll]
(if-not (empty? coll)
(if (condition (first coll))
(cons (first coll) (filterr condition (rest coll)))
(filterr (condition (rest coll))))))
(defn -main
"Main" []
(t/is (=
(filterr #(> % 2) [1 2 3 4 5 6])
[3 4 5 6])
"Failed basic test"))
但是,我的测试失败了
ERROR in () (Numbers.java:229)Failed basic testexpected: (= (filterr (fn* [p1__42#] (> p1__42# 2)) [1 2 3 4 5 6]) [3 4 5 6])
似乎这个函数没有被完全评估。
我不知道我做错了什么,我真的很感谢你在这件事上的帮助。
最佳答案
if语句的then子句中有一个额外的()集合。
(filterr (condition (rest coll)))
对
(filterr condition (rest coll))
关于clojure - Clojure-过滤器的用户实现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51473651/