我是 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"))
但是,我的测试因错误而失败
() 中的错误 (Numbers.java:229)
基础测试失败
预期:(= (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 - 过滤器的用户实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51473651/