我很難理解懶惰。Clojure中最簡單的懶惰功能
有人可以幫助我瞭解爲什麼我下面的功能更是不可以偷懶
(defn my-red
([f coll] (my-red f (first coll) (rest coll)))
([f init coll]
(let [mr (fn [g i c d]
(if (empty? c) d
(recur g (g i (first c)) (rest c) (conj d (g i (first c))))))]
(lazy-seq (mr f init coll [])))))
,而這個例子上clojure.org/lazy給出的是
(defn filter
"Returns a lazy sequence of the items in coll for which
(pred item) returns true. pred must be free of side-effects."
[pred coll]
(let [step (fn [p c]
(when-let [s (seq c)]
(if (p (first s))
(cons (first s) (filter p (rest s)))
(recur p (rest s)))))]
(lazy-seq (step pred coll))))