2

I am a newcomer to Clojure and one of the challenges suggested is a user-implementation of the Filter function.

So I came up with this

 (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"))

However, my test fails with the error

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])

It seems like the function isn't being evaluated fully.

I don't see what I'm doing wrong, and would really appreciate some help on the matter.

2 Answers 2

5

there's an extra set of ( ) in the then clause of the if statement.

(filterr (condition (rest coll)))

vs

(filterr condition (rest coll))
Sign up to request clarification or add additional context in comments.

Comments

2

The error is in this line (filterr (condition (rest coll))))))

You ought to have (filterr condition (rest coll))))) instead. Because (condition (rest coll)) make it a function call, while you simply need to pass this parameter to the next filterr call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.