(def even-nums [1 2 3 4 5 6 7 8])
(defn pairup [collection]
(loop [coll collection results []]
(if (> (count coll) 0)
(recur (drop 2 coll) (conj results (take 2 coll)))
results)))
user> (pairup even-nums)
[(1 2) (3 4) (5 6) (7 8)]
On Apr 29, 12:32 pm, "john.holland" <[email protected]> wrote:
> I'm pounding my head against the wall trying to understand how to do a
> simple task. What I want to do is write a function that will take a
> even-numbered set of numbers and split them into pairs.
>
> What I have right now is the following
>
> user> (defn pairup ([a b] [a b])([a b & rest] (cons (pairup a b)
> (apply pairup rest))) ([] [] ))
> #'user/pairup
> user> (pairup 1 2 3 4 5 6 7 8)
> ([1 2] [3 4] [5 6] 7 8)
> user>
>
> I can't get the last pair into a vector like the others.
>
> Can someone tell me what I am doing wrong? I know there is probably a
> way using the language to do this but I ended up trying to do it from
> scratch as a learning exercise.
>
> Thanks
>
> John
>
> --
> 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
> athttp://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