user=> (seq [])
nil
Why is nil returned, instead of an empty sequence?
Sean
I guess because a sequence is not a datastructure.
If there is nothing to iterate over via 'first or 'next, then it's nil.
And an "empty sequence" is precisely that: nil.
In my point of view, a sequence is more like a "functional iterator":
calling first on it will always return the same value, calling next on
it will return either nil if we are at the end, either a new seq ...
Hope I'm clear and also correct,
--
Laurent
2010/1/15 Sean Devlin <francoi...@gmail.com>:
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
user=> (seq [])
nil
Why is nil returned, instead of an empty sequence?
The background to this lies in the implementation of lazy sequences -
seq returns an implementation of ISeq for the data structure in
question - nil when there are no elements in the structure. Have a
look at http://clojure.org/sequences and also http://clojure.org/lazy
which gives the full reasoning behind this. It clarifies when to use
next and when to use rest (lazy sequences).
-Hth, Adrian.
> On Jan 15, 2010, at 3:26 AM, Sean Devlin wrote:
>
>> user=> (seq [])
>> nil
>>
>> Why is nil returned, instead of an empty sequence?
>
> There is no such thing as an empty seq.
This was true at one time, but isn't true after the changes to Clojure that added LazySeq.
For example:
user=> (def e (filter even? [1]))
#'user/e
user=> (type e)
clojure.lang.LazySeq ; e is a lazy seq
user=> (seq? e)
true ; e is a seq
user=> (empty? e)
true ; e is empty
user=> e
() ; e prints as an empty seq
> (seq x) (used as a predicate) is the canonical way in Clojure to ensure that x contains at least one item.
This is still true (and is mentioned in the doc for empty?)
user=> (seq e)
nil ; seq e is nil
--Steve