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?
Thanks!
shouxun
--
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