I am new to clojure and currently struggling with loop / recur
. The question basically is why my 'custom' range
func. does not return a lazy sequence. Is there something wrong with my implementation or you are not supposed to use recursion in this case?
(defn my-range
[nr]
(loop [l nr acc '()]
(if (< l 1)
acc
(recur (dec l) (conj acc l)))))
When I run it:
> (time (take 10 (my-range 100000))) ;; "Elapsed time: 85.443 msecs"
> (time (take 10 (range 100000))) ;; "Elapsed time: 0.095 msecs"
The very big time difference leads me to belive the list is first constructed and then 10 taken.
range
starts at0
, not1
. This does not affect the issue. – Thumbnail Mar 10 '16 at 15:16