Clojure - 过滤器的用户实现

标签 clojure functional-programming lisp

我是 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/

相关文章:

Clojure:命名匿名函数

java - 在maven项目中添加文件夹

java - Gen-Class 不生成 .class 文件

c++ - 这是以前做过的吗? (用于链式操作的 Monad View 包装 C++ 集合/类型)

haskell - FRP - 事件流和信号 - 仅使用信号会丢失什么?

scala - 为什么与空 fs2.Stream 合并会改变程序的行为

emacs - 在动态/按上下文自动插入之间切换

带有元数据的 Clojure 变量

lisp - 使用 Lisp : Passing a function to mapcar? 进行高阶编程

c - 在自己编写扩展之前要在 C 中实现多少 lisp?