--
You received this message because you are subscribed to the Google Groups "4Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
When I said “helper functions,” I just meant “user-written functions.” I call them helper functions because the solution to each 4clojure exercise is one function. Sorry for the confusion.
Using loop
etc. are a sign that you are writing low-level sequence manipulating code that probably can be replaced by one or two clojure.core functions. E.g.
solution by beginner used to imperative programming:
(fn solution [coll]
(Iet-fn [(filter-out-nils [xs]
(loop [res [] xs xs]
(if (empty? xs) ; done
res
(let [[x & r] xs]
(if (nil? x)
(recur res r) ; don't want nils
(recur (conj res x) r))))))] ; add x to end of result, recur on rest
... (filter-out-nils coll) ...))
solution once you know more clojure:
(fn solution [coll]
... (remove nil? coll) ...)
Sometimes an exercise makes you re-implement a core function using loop
, lazy-seq
, etc, but otherwise they are not usually needed.