【问题标题】:Clojure - User implementation of FilterClojure - 过滤器的用户实现
【发布时间】:2018-12-30 14:27:08
【问题描述】:

我是 Clojure 的新手,建议的挑战之一是过滤器功能的用户实现。

所以我想出了这个

 (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 test expected: (= (filterr (fn* [p1__42#] (> p1__42# 2)) [1 2 3 4 5 6]) [3 4 5 6])

似乎没有对函数进行全面评估。

我看不出我做错了什么,并且非常感谢在这件事上提供一些帮助。

【问题讨论】:

    标签: clojure functional-programming lisp


    【解决方案1】:

    错误在这一行(filterr (condition (rest coll))))))

    你应该改用(filterr condition (rest coll)))))。因为(condition (rest coll)) 使它成为一个函数调用,而您只需将此参数传递给下一个过滤器调用。

    【讨论】:

      【解决方案2】:

      在 if 语句的 then 子句中有一组额外的 ( )。

      (filterr (condition (rest coll)))
      

      (filterr condition (rest coll))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-26
        • 1970-01-01
        • 2014-05-06
        • 2010-11-10
        • 2020-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多