在 2011-8-26 下午11:44,"Tassilo Horn" <[email protected]>写道:
>
> Shouxun Yang <[email protected]> writes:
>
> > (defn my-flatten
> >   [coll]
> >   (loop [acc [] coll coll]
> >     (if-let [[a & coll] coll]
> >       (if (or (list? a) (vector? a))
> >           (recur acc (if (empty? coll)
> >                        (vec a)
> >                        (conj (vec a) coll)))
> >           (recur (conj acc a) coll))
> >       acc)))
> >
> > The above code failed the second test: ["a" ["b"] "c"].
> >
> > Instead of producing the correct result, it produces: ["a" "b" ("c")].
>
> The problem is that in ["a" "b" ("c")], ("c") is neither a list nor a
> vector.  It's a seq.  Unfortunately, seqs and lists have the same print
> syntax, so you were not able to easily see that.
>
> BTW: You should use coll? instead of sequential? or seq? if your
> intention is only to check if the thing is some collection.  For
> example, (sequential? #{1 2 3}) is false.  (sequential? only works for
> ordered collections like vectors, lists and seqs.)

Thanks. For this case, I actually need an atom? predicate as in CL.

> BTW2: Instead of (empty? coll), use the idiomatic (seq coll) recipe.

Good point. I read about the tip before, but forgot in my own code.

> Bye,
> Tassilo

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to