On Fri, Apr 30, 2010 at 2:30 PM, Michael Wood wrote:
> On 30 April 2010 18:25, Mark J. Reed wrote:
> [...]
>> (defn pairup
>> ([a b] (list [a b]))
>> ([a b & rest] (cons [a b] (apply pairup rest
Why not:
(defn pairup
( [] nil )
( [ a b & rest ] ( cons [ a b ] ( apply pairu
(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" wrote:
>
much thanks for the insight-giving answers from you all
On Apr 30, 12:29 pm, "Mark J. Reed" wrote:
> On Fri, Apr 30, 2010 at 12:25 PM, Mark J. Reed wrote:
>
> > 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])
Um, got a typo in there; "up to 2x that time" was meant to be "up to
3x that time".
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please b
On 1 May 2010 03:19, Mark J. Reed wrote:
>> Another version:
>>
>> (defn pairup [& args]
>> (map vector args (rest args)))
>
> Nope, that doubles the middle elements:
> user=> (pairup 1 2 3 4)
> ([1 2] [2 3] [3 4])
Ouch, right, forgot to include take-nth:
(defn pairup [& args]
(take-nth 2 (ma
0On Fri, Apr 30, 2010 at 8:41 PM, Michał Marczyk
wrote:
> That will overflow the stack when you do, say,
>
> (last (apply pairup (range 2)))
>
> That can be fixed by wrapping (cons ...) in lazy-seq.
>
Sure. Laziness good.
Another version:
>
> (defn pairup [& args]
> (map vector args (re
On 30 April 2010 18:25, Mark J. Reed wrote:
> (defn pairup
> ([a b] (list [a b]))
> ([a b & rest] (cons [a b] (apply pairup rest
That will overflow the stack when you do, say,
(last (apply pairup (range 2)))
That can be fixed by wrapping (cons ...) in lazy-seq.
Another versio
Ok, so I was right the first time. It think it's past everyone's bedtime.
:)
On Fri, Apr 30, 2010 at 5:49 PM, Douglas Philips wrote:
> On 2010 Apr 30, at 5:45 PM, Mark J. Reed wrote:
>
>> Of course. Which is what I would have done automatically with a
>> Lispier construct. Just still not used
On 30 April 2010 23:49, Douglas Philips wrote:
> On 2010 Apr 30, at 5:45 PM, Mark J. Reed wrote:
>>
>> Of course. Which is what I would have done automatically with a
>> Lispier construct. Just still not used to seeing literal vectors as
>> functions. :)
>>
>> On Friday, April 30, 2010, Michael
On 2010 Apr 30, at 5:45 PM, Mark J. Reed wrote:
Of course. Which is what I would have done automatically with a
Lispier construct. Just still not used to seeing literal vectors as
functions. :)
On Friday, April 30, 2010, Michael Wood wrote:
Well, you didn't *have* to call list. You could hav
Of course. Which is what I would have done automatically with a
Lispier construct. Just still not used to seeing literal vectors as
functions. :)
On Friday, April 30, 2010, Michael Wood wrote:
> On 30 April 2010 18:25, Mark J. Reed wrote:
> [...]
>> (defn pairup
>> ([a b] (list [a b]))
>>
On 30 April 2010 18:25, Mark J. Reed wrote:
[...]
> (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.IllegalArgumen
On Fri, Apr 30, 2010 at 12:25 PM, Mark J. Reed wrote:
> 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)
>
To be cle
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
I'd use the built-in, partition
user=> (partition 2 (range 1 9))
((1 2) (3 4) (5 6) (7 8))
And add a mapping operation
user=> (map vec (partition 2 (range 1 9)))
([1 2] [3 4] [5 6] [7 8])
Am I missing a requirement?
On Apr 30, 11:55 am, "Mark J. Reed" wrote:
> I think you want this:
>
>
I think you want this:
(defn pairup [a b & rest] (cons [a b] (if rest (apply pairup rest) [])))
On Thu, Apr 29, 2010 at 3:32 PM, john.holland 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
>
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
17 matches
Mail list logo