From: Tamas K Papp on
Hi,

I am trying to rewrite GROUP from On Lisp without recursion (just as an
exercise). My first attempt is below, I am wondering if there is a
nicer (more idiomatic, etc) way to do it.

(defun group (list n)
"Return elements of LIST as a list of lists in groups of N."
(check-type n (integer 1))
(let (sublist
result
(i 0))
(dolist (element list)
(push element sublist)
(incf i)
(when (= i n)
(push (nreverse sublist) result)
(setf i 0
sublist nil)))
(assert (zerop i) () "~A could not be broken up to sublists of ~A elements" list n)
(nreverse result)))

Thanks,

Tamas
From: Rainer Joswig on
In article <7vufvgFifkU1(a)mid.individual.net>,
Tamas K Papp <tkpapp(a)gmail.com> wrote:

> Hi,
>
> I am trying to rewrite GROUP from On Lisp without recursion (just as an
> exercise). My first attempt is below, I am wondering if there is a
> nicer (more idiomatic, etc) way to do it.
>
> (defun group (list n)
> "Return elements of LIST as a list of lists in groups of N."
> (check-type n (integer 1))
> (let (sublist
> result
> (i 0))
> (dolist (element list)
> (push element sublist)
> (incf i)
> (when (= i n)
> (push (nreverse sublist) result)
> (setf i 0
> sublist nil)))
> (assert (zerop i) () "~A could not be broken up to sublists of ~A elements" list n)
> (nreverse result)))
>
> Thanks,
>
> Tamas

Nicer? Let's see...


(defun group (list n)
"Return elements of LIST as a list of lists in groups of N."
(check-type n (integer 1))
(loop with list1 = list
while list1 collect
(loop repeat n
unless list1
do (error "~A could not be broken up to sublists of ~A elements" list n)
collect (pop list1))))

--
http://lispm.dyndns.org/
From: Tamas K Papp on
On Fri, 12 Mar 2010 10:44:54 +0100, Rainer Joswig wrote:

> In article <7vufvgFifkU1(a)mid.individual.net>,
> Tamas K Papp <tkpapp(a)gmail.com> wrote:
>
>> Hi,
>>
>> I am trying to rewrite GROUP from On Lisp without recursion (just as an
>> exercise). My first attempt is below, I am wondering if there is a
>> nicer (more idiomatic, etc) way to do it.
>>
>> (defun group (list n)
>> "Return elements of LIST as a list of lists in groups of N."
>> (check-type n (integer 1))
>> (let (sublist
>> result
>> (i 0))
>> (dolist (element list)
>> (push element sublist)
>> (incf i)
>> (when (= i n)
>> (push (nreverse sublist) result)
>> (setf i 0
>> sublist nil)))
>> (assert (zerop i) () "~A could not be broken up to sublists of ~A
>> elements" list n) (nreverse result)))
>>
>> Thanks,
>>
>> Tamas
>
> Nicer? Let's see...
>
>
> (defun group (list n)
> "Return elements of LIST as a list of lists in groups of N."
> (check-type n (integer 1))
> (loop with list1 = list
> while list1 collect
> (loop repeat n
> unless list1
> do (error "~A could not be broken up to sublists of ~A
> elements" list n) collect (pop list1))))

Very neat, thanks! I didn't think of popping elements like that, but it
makes sense.

Tamas
From: Rainer Joswig on
In article <7vum6aFifkU2(a)mid.individual.net>,
Tamas K Papp <tkpapp(a)gmail.com> wrote:

> On Fri, 12 Mar 2010 10:44:54 +0100, Rainer Joswig wrote:
>
> > In article <7vufvgFifkU1(a)mid.individual.net>,
> > Tamas K Papp <tkpapp(a)gmail.com> wrote:
> >
> >> Hi,
> >>
> >> I am trying to rewrite GROUP from On Lisp without recursion (just as an
> >> exercise). My first attempt is below, I am wondering if there is a
> >> nicer (more idiomatic, etc) way to do it.
> >>
> >> (defun group (list n)
> >> "Return elements of LIST as a list of lists in groups of N."
> >> (check-type n (integer 1))
> >> (let (sublist
> >> result
> >> (i 0))
> >> (dolist (element list)
> >> (push element sublist)
> >> (incf i)
> >> (when (= i n)
> >> (push (nreverse sublist) result)
> >> (setf i 0
> >> sublist nil)))
> >> (assert (zerop i) () "~A could not be broken up to sublists of ~A
> >> elements" list n) (nreverse result)))
> >>
> >> Thanks,
> >>
> >> Tamas
> >
> > Nicer? Let's see...
> >
> >
> > (defun group (list n)
> > "Return elements of LIST as a list of lists in groups of N."
> > (check-type n (integer 1))
> > (loop with list1 = list
> > while list1 collect
> > (loop repeat n
> > unless list1
> > do (error "~A could not be broken up to sublists of ~A
> > elements" list n) collect (pop list1))))
>
> Very neat, thanks! I didn't think of popping elements like that, but it
> makes sense.
>
> Tamas

Slightly improved:

(defun group (list n &aux (orig-list list))
"Return elements of LIST as a list of lists in groups of N."
(check-type n (integer 1))
(loop while list collect
(loop repeat n
unless list
do (error "~A could not be broken up to sublists of ~A elements" orig-list n)
collect (pop list))))

--
http://lispm.dyndns.org/
From: Jochen Schmidt on
(defun group (list n)
(loop for e in list
for i upfrom 1
collect e into group
if (zerop (mod i 5))
collect (copy-list group) and do (setf group nil)))

--
Jochen Schmidt
CRISPYLOGICS
Uhlandstr. 9, 90408 Nuremberg

Fon +49 (0)911 517 999 82
Fax +49 (0)911 517 999 83

mailto:(format nil "~(~36r@~36r.~36r~)" 870180 1680085828711918828 16438)
http://www.crispylogics.com