nob*_*ere 2 lisp functional-programming clojure
我是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"))
Run Code Online (Sandbox Code Playgroud)
但是,我的测试因错误而失败
ERROR in () (Numbers.java:229)
Failed basic test
expected: (= (filterr (fn* [p1__42#] (> p1__42# 2)) [1 2 3 4 5 6])
[3 4 5 6])
看起来这个功能没有得到充分的评估.
我不明白我做错了什么,并且非常感谢你对此事的一些帮助.
在if语句的then子句中有一组额外的().
(filterr (condition (rest coll)))
Run Code Online (Sandbox Code Playgroud)
VS
(filterr condition (rest coll))
Run Code Online (Sandbox Code Playgroud)