Michael, thank you very much. I never paid much attention to the chunked seq feature before. The effect is surprising to my Common Lisp / OCaml background.
I thought my code good enough to cover the test cases. Always new thing to learn. shouxun 在 2011-8-26 下午11:42,"Michael Wood" <[email protected]>写道: > Hi > > On 25 August 2011 18:16, Shouxun Yang <[email protected]> wrote: >> Dear Clojurians, >> >> while playing with http://4clojure.com I found a perplexing behavior >> when I submit my own code for implementing my own poor implementation >> of my-flatten for problem 28: >> >> (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")]. >> >> But if I replace "(or (list? a) (vector? a))" with "(sequential? a)", >> the code works as expected. >> >> (defn my-flatten2 >> [coll] >> (loop [acc [] coll coll] >> (if-let [[a & coll] coll] >> (if (sequential? a) >> (recur acc (if (empty? coll) >> (vec a) >> (conj (vec a) coll))) >> (recur (conj acc a) coll)) >> acc))) >> >> While "(or (list? a) (vector? a))" will evaluates to true given a >> being '(c), somewhow in the above function it was evaluated to being >> false. >> >> Can anyone help me understand what is going on here? > > Does this help? > > user=> (class '("c")) > clojure.lang.PersistentList > user=> (if-let [[a & coll] coll] coll) > (["b"] "c") > user=> (class (if-let [[a & coll] coll] coll)) > clojure.lang.PersistentVector$ChunkedSeq > user=> (list? (if-let [[a & coll] coll] coll)) > false > user=> (vector? (if-let [[a & coll] coll] coll)) > false > > -- > Michael Wood <[email protected]> > > -- > 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 -- 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
