It's great to see so many people learning Clojure this week :) starting with a fundamental problem like this is a really good start. Hamza and Jonas's answers clearly cover the original question quite well. I would like to offer some unsolicited advice on where to take it from here in the hopes that it will be helpful.
Once you have the base recursive form you can turn it into idiomatic Clojure generally by:
1) use tail recursive forms when you can (you already did this)
2) replace direct recursion with the recur
call to keep from blowing the stack. (starting with Hamza's working answer)
(defn my-even? [coll]
(if-let [[first & rest] coll]
(if (= (mod first 2) 0)
(cons first (my-even? rest))
(recur rest))))
the recur
causes the compiler to jump to the start of the stack frame instead of allocating a new one. without this it will blow the stack.
3) in many cases you can eliminate the (defn [] ... (recur))
pattern with a higher order function like map
, reduce
, filter
, for
, etc. In this excercise I see you are trying to not use filter
or even
, so obviously you could write my-filter and my-even and that would be ok ;)
4) extract the divisible parts, (building a list, choosing what to include) into reusable functions and upload any that are generally useful to a clojure contrib project :)
5) think very carefully should you find yourself using (lazy-seq ...)
as there is a good chance you are re-inventing the wheel.