Sorry, let me try answering your questions instead of just proposing an
alternative. :)
As written, your base case and your recursive call are inconsistent. If you
want (pairup 1 2 3 4) to return ([1 2] [3 4]), then you need to wind up
calling (cons [1 2] [[3 4]]). What you're calling instead is (cons [1 2] [3
4]), because (pairup 3 4) returns a lone pair instead of a list containing a
single pair.
Basically, you can't call (cons (pairup) (pairup)) because the two arguments
to (cons) are different: the first is an item, and the second is a list to
cons the item onto.
So the fix is to make the base case return ([a b]) instead of just [a b],
and not to use recursion in the first argument to cons in your recurse case:
(defn pairup
([a b] (list [a b]))
([a b & rest] (cons [a b] (apply pairup rest))))
I got an error when I tried ([a b]) instead of (list [a b]), by the way:
Clojure 1.1.0
user=> (cons [1 2] ([3 4]))
java.lang.IllegalArgumentException: Wrong number of args passed to:
PersistentVector (NO_SOURCE_FILE:0)
On Thu, Apr 29, 2010 at 3: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]<clojure%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
--
Mark J. Reed <[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