Clojure and me has moved.

Monday, February 23, 2009

rest vs drop

This post has moved, go to its new location
Now that fully lazy sequences are in the trunk, (rest a-seq) and (drop 1 a-seq) aren't equivalent anymore:
user=> (def s (map #(do (println %) %) (range 10)))
#'user/s
user=> (def d (drop 1 s))
#'user/d
user=> (def r (rest s))
0
#'user/r

As one can see rest needs to realize the first element of s while drop doesn't. The corollary is that (drop n s) holds on the whole seq (including the nth first elements) while rest computes the first element and then discards it.

No comments: