On Tue, Jun 23, 2009 at 5:22 PM, pupsik <[email protected]> wrote:
> (defn my-zipmap [keys vals]
> (loop [my-map {}
> my-keys (seq keys)
> my-vals (seq vals)]
> (if (and my-keys my-vals)
> (recur (assoc my-map (first my-keys) (first my-vals))
> (rest my-keys)
> (rest my-vals))
> my-map)))
>
Well, it seems like the for loop never terminates. (rest ()) => () and as
far as I can tell an empty sequence is logically true, not false. (if (rest
()) 1 2) => 1 so your if will never hit the else clause.
Here's a version that will do what you expect.
(defn my-zipmap [keys vals]
(loop [my-map {}
my-keys (seq keys)
my-vals (seq vals)]
(if-not (or (empty? my-keys) (empty? my-vals))
(recur (assoc my-map (first my-keys) (first my-vals))
(rest my-keys)
(rest my-vals))
my-map)))
user=> (my-zipmap [:a :b :c] [1 2 3])
{:c 3, :b 2, :a 1}
--
Cosmin Stejerean
http://offbytwo.com
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---