On Dec 6, 2008, at 10:27 PM, Paul Mooser wrote:

I think I understand. The lazy-cons that filter is constructing
maintains a reference to the whole coll in its tail so that it can
evaluate (rest coll) when it is forced. Hmmm!

In the current implementation of filter, coll is held the entire time that (rest coll) is calculated on the first match.

The following separation into two functions appears to solve it. I'll be looking at simplifying it.

If you use a definition of filter like this in your test, I think it will succeed:

(defn filter-iter
  [pred coll]
  (when (seq coll)
    (if (pred (first coll))
      [(first coll) (rest coll)]
      (recur pred (rest coll)))))

(defn filter
  "Returns a lazy seq of the items in coll for which
  (pred item) returns true. pred must be free of side-effects."
  [pred coll]
  (let [result (filter-iter pred coll)]
    (when result
      (lazy-cons (result 0) (result 1)))))

--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to