Re: something stupid I'm trying to do

2010-05-01 Thread Sean Corfield
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

Re: something stupid I'm trying to do

2010-05-01 Thread devender
(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: >

Re: something stupid I'm trying to do

2010-05-01 Thread john.holland
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])

Re: something stupid I'm trying to do

2010-05-01 Thread Michał Marczyk
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

Re: something stupid I'm trying to do

2010-05-01 Thread Michał Marczyk
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

Re: something stupid I'm trying to do

2010-04-30 Thread Mark J. Reed
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

Re: something stupid I'm trying to do

2010-04-30 Thread Michał Marczyk
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

Re: something stupid I'm trying to do

2010-04-30 Thread Mark J. Reed
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

Re: something stupid I'm trying to do

2010-04-30 Thread Michael Wood
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

Re: something stupid I'm trying to do

2010-04-30 Thread Douglas Philips
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

Re: something stupid I'm trying to do

2010-04-30 Thread Mark J. Reed
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])) >>

Re: something stupid I'm trying to do

2010-04-30 Thread Michael Wood
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

Re: something stupid I'm trying to do

2010-04-30 Thread Mark J. Reed
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

Re: something stupid I'm trying to do

2010-04-30 Thread Mark J. Reed
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

Re: something stupid I'm trying to do

2010-04-30 Thread Sean Devlin
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: > >  

Re: something stupid I'm trying to do

2010-04-30 Thread Mark J. Reed
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 >

something stupid I'm trying to do

2010-04-30 Thread john.holland
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