From: dvlfrnd
Subject: newbie question
Date: 
Message-ID: <1136059463.523048.214540@g49g2000cwa.googlegroups.com>
hi there,
here's a newbie question
im not a programmer and lisp is the only language i've ever worked
with,
but somehow i love it and i wanna learn.
i was giving myself exercises and came up with this,pardon my
programming inefficency and please help.
say there's a list of even length
(a b c d e f ......)

how do i turn this into
((a b) (c d) (e f)....)

simply how do i couple the elements?

i first tried to write a function:

(defun couple-first-two (lst)
  (list (first lst) (second lst)))

but couldn't find a way to apply it to the entire list.
i tried a recursive function,but it didnt give the output i wanted.

then i tried this

(defun pair-add-element (n lst)
  (mapcar #'(lambda (x) (list n x)) lst))

if i could split the list into two,
where the original is (a b c d e f)
to
(a c e) and (b d f)

i could mapcar these two new lists using pair-add-element function
above
and return this
((a b) (c d) (e f))

so the i have two questions

1- how do i split a list into two, taking even'th and odd'th elemtns
into separate lists
2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
(e f)) in an efficient,proper code

how's my approach?? is it too silly, i know i have tons to learn about
programming, please show me a way to solve this, including the code and
the approach,design etc...

thank you very much 
and happy new year

From: JP Massar
Subject: Re: newbie question
Date: 
Message-ID: <iqqdr15s4idgkammlb49j5jtf1p6idgj38@4ax.com>
On 31 Dec 2005 12:04:23 -0800, "dvlfrnd" <·······@yahoo.com> wrote:
 
>say there's a list of even length
>(a b c d e f ......)
>
>how do i turn this into
>((a b) (c d) (e f)....)
 
(loop for x on list by 'cddr collect (list (first x) (second x)))
From: OMouse
Subject: Re: newbie question
Date: 
Message-ID: <1136067396.949454.19320@g49g2000cwa.googlegroups.com>
> 1- how do i split a list into two, taking even'th and odd'th elemtns
> into separate lists

(defun split-list(myList)
(let (evenList nil) (oddList nil)
  (loop for i from 1 to (list-length myList) do
  (if (eq (oddp i) 't) (setq (append oddList (
  (values evenList oddList))
)
That's what i have so far. I'll finish it off later. But the other one
is easy enough.

> 2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
> (e f)) in an efficient,proper code

;Adds to the end of the list and returns the new list
(defun add-to-list(blah blah2 myList)
  (append myList (list (list blah blah2))))

;Example
(setq myList nil)
(setq myList (add-to-list 'a 'b myList))
(setq myList (add-to-list 'c 'd myList))
(setq myList (add-to-list 'e 'f myList))
;There you go
From: airfoyle
Subject: Re: newbie question
Date: 
Message-ID: <1136074782.966872.293930@g43g2000cwa.googlegroups.com>
[······@gmail.com]
>
> (defun split-list(myList)
> (let (evenList nil) (oddList nil)
>   (loop for i from 1 to (list-length myList) do
>   (if (eq (oddp i) 't) (setq (append oddList (
>   (values evenList oddList))
> )
> That's what i have so far. I'll finish it off later. But the other one
> is easy enough.

Your solution has bugs, but it's misguided through and through.  You
shouldn't walk through a list by continually getting the i'th element,
and you shouldn't build a list by appending to its end.

Here's the right way to do it --

(defun split-list (l)
   (cond ((null l)
          (values '() '()))
         (t
          (multiple-value-bind (evens-from-cdr odds-from-cdr)
                               (split-list (cdr l))
             (values (cons (car l) odds-from-cdr)
                     evens-from-cdr)))))

                                        -- Drew McDermott
                                           Yale Computer Science
Department
From: ········@gmail.com
Subject: Re: newbie question
Date: 
Message-ID: <1136076221.348079.24750@z14g2000cwz.googlegroups.com>
>1. dvlfrnd
>Dec 31, 12:04 pm   show options
>hi there,
>here's a newbie question
>im not a programmer and lisp is the only language i've ever worked
>with,
>but somehow i love it and i wanna learn.

Hey dvlfrnd,

I recommend checking out Abelson and Sussman's SICP lectures

    http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/

which are free to download. Also maybe The Little Schemer and anything
you can get your hands on about functional programming if you are just
getting started programming. As for your question, the copy-list
example in this page

   http://www.lisp.org/table/style.htm#efficiency

might give you some ideas to think about.

Nick
From: Kenny Tilton
Subject: Re: newbie question
Date: 
Message-ID: <3ECtf.23599$GW1.7748@news-wrt-01.rdc-nyc.rr.com>
dvlfrnd wrote:
> hi there,
> here's a newbie question
> im not a programmer and lisp is the only language i've ever worked
> with,
> but somehow i love it and i wanna learn.
> i was giving myself exercises and came up with this,pardon my
> programming inefficency and please help.
> say there's a list of even length
> (a b c d e f ......)
> 
> how do i turn this into
> ((a b) (c d) (e f)....)
> 
> simply how do i couple the elements?
> 
> i first tried to write a function:
> 
> (defun couple-first-two (lst)
>   (list (first lst) (second lst)))
> 
> but couldn't find a way to apply it to the entire list.
> i tried a recursive function,but it didnt give the output i wanted.

What was your failed recursive solution? Post the code and we can help 
get you used to recursion.


kt
From: dvlfrnd
Subject: Re: newbie question
Date: 
Message-ID: <1136082898.112697.115840@g43g2000cwa.googlegroups.com>
thanks for the replies!!!
here's the code i tried

(setf qq nil)

(defun addon (lst)
  (cond ((null lst) qq)
        (t (setf qq (list (pair lst)
                          (addon (rest(rest lst))))))))

(defun pair (lst)
  (list (first lst) (second lst)))

it looks buggy from scratch,i knew it, thats why i wanted to mapcar two
separate lists (a c e) and (b d f) pairing them
thanks again and again, i will dig the codes and links right after the
new year party
i wish everyone a very happy new year
From: Timofei Shatrov
Subject: Re: newbie question
Date: 
Message-ID: <43b7a1f3.2257351@news.readfreenews.net>
On 31 Dec 2005 19:01:30 -0800, "dvlfrnd" <·······@yahoo.com> tried to
confuse everyone with this message:

>thanks for the replies!!!
>here's the code i tried
>
>(setf qq nil)
>
>(defun addon (lst)
>  (cond ((null lst) qq)
>        (t (setf qq (list (pair lst)
>                          (addon (rest(rest lst))))))))
>

Your list would consist only of two elements (because list function only
has two arguments). What you want is to add an element to the beginning
of the list. The hack is to use cons: 

(cons (pair lst) (addon (rest (rest lst))))

cons is a low-level function for constructing lists from scratch. You
may want to read about the internal structure of lists, because it may
be quite useful (like in that example).

-- 
|a\o/r|,-------------.,---------- Timofei Shatrov aka Grue ------------.
| m"a ||FC AMKAR PERM|| mail: grue at mail.ru  http://grue3.tripod.com |
|  k  ||  PWNZ J00   || Kingdom of Loathing: Grue3 lvl 18 Seal Clubber |
`-----'`-------------'`-------------------------------------------[4*72]
From: Frode Vatvedt Fjeld
Subject: Re: newbie question
Date: 
Message-ID: <2hvex52ebc.fsf@vserver.cs.uit.no>
"dvlfrnd" <·······@yahoo.com> writes:

> say there's a list of even length
> (a b c d e f ......)
> 
> how do i turn this into
> ((a b) (c d) (e f)....)

The mapping idiom doesn't work so well here, since that's all about
transforming each element individually, whereas here you process
elements two by two (or the list as such in steps of two).

I think this is the most natural solution in Common Lisp:

  (loop for (x y) on list by #'cddr collect (list x y))

-- 
Frode Vatvedt Fjeld
From: Coby Beck
Subject: Re: newbie question
Date: 
Message-ID: <bZCtf.24156$6K2.2137@edtnps90>
"dvlfrnd" <·······@yahoo.com> wrote in message 
·····························@g49g2000cwa.googlegroups.com...

> but couldn't find a way to apply it to the entire list.
> i tried a recursive function,but it didnt give the output i wanted.

Here is a recursive solution to add to the loops:

(defun divide-in-pairs (list)
  (cond ((not (listp list)) (error "non-list argument"))
        ((null list) nil)
        ((null (cdr list))  (error "odd number of elements"))
        ((null (cddr list)) (list list))
        (t (cons (list (first list) (second list))
                 (divide-in-pairs (cddr list))))))

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Kenny Tilton
Subject: Re: newbie question
Date: 
Message-ID: <2NOtf.23623$GW1.10718@news-wrt-01.rdc-nyc.rr.com>
Coby Beck wrote:
> "dvlfrnd" <·······@yahoo.com> wrote in message 
> ·····························@g49g2000cwa.googlegroups.com...
> 
> 
>>but couldn't find a way to apply it to the entire list.
>>i tried a recursive function,but it didnt give the output i wanted.
> 
> 
> Here is a recursive solution to add to the loops:
> 
> (defun divide-in-pairs (list)
>   (cond ((not (listp list)) (error "non-list argument"))
>         ((null list) nil)
>         ((null (cdr list))  (error "odd number of elements"))
>         ((null (cddr list)) (list list))
>         (t (cons (list (first list) (second list))
>                  (divide-in-pairs (cddr list))))))
> 

Damn the torpedos. Real programmers do not validate their input.

kt
From: Pascal Bourguignon
Subject: Re: newbie question
Date: 
Message-ID: <87k6dkyx1l.fsf@thalassa.informatimago.com>
Kenny Tilton <·············@nyc.rr.com> writes:

> Coby Beck wrote:
>> "dvlfrnd" <·······@yahoo.com> wrote in message
>> ·····························@g49g2000cwa.googlegroups.com...
>> 
>>>but couldn't find a way to apply it to the entire list.
>>>i tried a recursive function,but it didnt give the output i wanted.
>> Here is a recursive solution to add to the loops:
>> (defun divide-in-pairs (list)
>>   (cond ((not (listp list)) (error "non-list argument"))
>>         ((null list) nil)
>>         ((null (cdr list))  (error "odd number of elements"))
>>         ((null (cddr list)) (list list))
>>         (t (cons (list (first list) (second list))
>>                  (divide-in-pairs (cddr list))))))
>> 
>
> Damn the torpedos. Real programmers do not validate their input.

Well, not exactly.  Real lisp programmers rely on the lisp functions
to validate their input, and accept their behavior in border cases as
useful:


[24]> (defun divide-in-pairs (list)
   (when list
       (cons (list (first list) (second list))
             (divide-in-pairs (cddr list)))))

DIVIDE-IN-PAIRS
[25]> (divide-in-pairs 'toto)

*** - FIRST: TOTO is not a list
The following restarts are available:
ABORT          :R1      ABORT
Break 1 [26]> :q
[28]> (divide-in-pairs '(a b c d))
((A B) (C D))
[29]> (divide-in-pairs '(a b c))
((A B) (C NIL))
[31]> (divide-in-pairs '(a b c . d))

*** - SECOND: D is not a list
The following restarts are available:
ABORT          :R1      ABORT
Break 1 [32]> (divide-in-pairs '(a b c d . e))

*** - FIRST: E is not a list
The following restarts are available:
ABORT          :R1      ABORT
ABORT          :R2      ABORT
Break 2 [33]> :q

etc.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
I need a new toy.
Tail of black dog keeps good time.
Pounce! Good dog! Good dog!
From: Coby Beck
Subject: Re: newbie question
Date: 
Message-ID: <sUWtf.19505$km.14606@edtnps89>
"Pascal Bourguignon" <····@mouse-potato.com> wrote in message 
···················@thalassa.informatimago.com...
> Kenny Tilton <·············@nyc.rr.com> writes:
>
>> Coby Beck wrote:
>>> "dvlfrnd" <·······@yahoo.com> wrote in message
>>> ·····························@g49g2000cwa.googlegroups.com...
>>>
>>>>but couldn't find a way to apply it to the entire list.
>>>>i tried a recursive function,but it didnt give the output i wanted.
>>> Here is a recursive solution to add to the loops:
>>> (defun divide-in-pairs (list)
>>>   (cond ((not (listp list)) (error "non-list argument"))
>>>         ((null list) nil)
>>>         ((null (cdr list))  (error "odd number of elements"))
>>>         ((null (cddr list)) (list list))
>>>         (t (cons (list (first list) (second list))
>>>                  (divide-in-pairs (cddr list))))))
>>>
>>
>> Damn the torpedos. Real programmers do not validate their input.
>
> Well, not exactly.  Real lisp programmers rely on the lisp functions
> to validate their input, and accept their behavior in border cases as
> useful:

Damn.  Either way you slice it, I'm screwed!

> [25]> (divide-in-pairs 'toto)
> *** - FIRST: TOTO is not a list
> [29]> (divide-in-pairs '(a b c))
> ((A B) (C NIL))
> [31]> (divide-in-pairs '(a b c . d))
> *** - SECOND: D is not a list

I actually agree those are reasonable behaviours for a general purpose 
utility.

I guess I was coding as if there was some underlying specific purpose as it 
seems a rather arbitrary task.  So maybe in the real app it would be

(defun assemble-grapple-gromets (new-parts-delivery)
  (cond ((not (listp new-parts-delivery)) (error "not a bag of parts"))
        ((null new-parts-delivery) nil)
        ((null (cdr new-parts-delivery))  (error "missing a falangee"))
        ((null (cddr new-parts-delivery)) (list new-parts-delivery))
        (t (cons (list (first new-parts-delivery) (second 
new-parts-delivery))
                 (assemble-grapple-gromets (cddr new-parts-delivery))))))

Though that still won't make Kenny happy ;)

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Kenny Tilton
Subject: Re: newbie question
Date: 
Message-ID: <9t5uf.34707$i1.18309@news-wrt-01.rdc-nyc.rr.com>
Coby Beck wrote:
> "Pascal Bourguignon" <····@mouse-potato.com> wrote in message 
> ···················@thalassa.informatimago.com...
> 
>>Kenny Tilton <·············@nyc.rr.com> writes:
>>
>>
>>>Coby Beck wrote:
>>>
>>>>"dvlfrnd" <·······@yahoo.com> wrote in message
>>>>·····························@g49g2000cwa.googlegroups.com...
>>>>
>>>>
>>>>>but couldn't find a way to apply it to the entire list.
>>>>>i tried a recursive function,but it didnt give the output i wanted.
>>>>
>>>>Here is a recursive solution to add to the loops:
>>>>(defun divide-in-pairs (list)
>>>>  (cond ((not (listp list)) (error "non-list argument"))
>>>>        ((null list) nil)
>>>>        ((null (cdr list))  (error "odd number of elements"))
>>>>        ((null (cddr list)) (list list))
>>>>        (t (cons (list (first list) (second list))
>>>>                 (divide-in-pairs (cddr list))))))
>>>>
>>>
>>>Damn the torpedos. Real programmers do not validate their input.
>>
>>Well, not exactly.  Real lisp programmers rely on the lisp functions
>>to validate their input, and accept their behavior in border cases as
>>useful:
> 
> 
> Damn.  Either way you slice it, I'm screwed!
> 
> 
>>[25]> (divide-in-pairs 'toto)
>>*** - FIRST: TOTO is not a list
>>[29]> (divide-in-pairs '(a b c))
>>((A B) (C NIL))
>>[31]> (divide-in-pairs '(a b c . d))
>>*** - SECOND: D is not a list
> 
> 
> I actually agree those are reasonable behaviours for a general purpose 
> utility.
> 
> I guess I was coding as if there was some underlying specific purpose as it 
> seems a rather arbitrary task.  So maybe in the real app it would be
> 
> (defun assemble-grapple-gromets (new-parts-delivery)
>   (cond ((not (listp new-parts-delivery)) (error "not a bag of parts"))
>         ((null new-parts-delivery) nil)
>         ((null (cdr new-parts-delivery))  (error "missing a falangee"))
>         ((null (cddr new-parts-delivery)) (list new-parts-delivery))
>         (t (cons (list (first new-parts-delivery) (second 
> new-parts-delivery))
>                  (assemble-grapple-gromets (cddr new-parts-delivery))))))
> 
> Though that still won't make Kenny happy ;)
> 

Actually, validating input is fine if one wants to spend a solid 
fraction of time typing in validation, and maintaining it as one 
refactors. It just reminds me of the classic Lisp trade-off of 
guaranteed correctness vs programmer productivity, as when we sniff at 
strong static typing. And the punch line is the same: the same QA can be 
achieved without the strait jackets, methinks.

kt
From: Coby Beck
Subject: Re: newbie question
Date: 
Message-ID: <aQeuf.24281$AP5.17739@edtnps84>
"Kenny Tilton" <·············@nyc.rr.com> wrote in message 
·························@news-wrt-01.rdc-nyc.rr.com...
> Coby Beck wrote:
>> "Pascal Bourguignon" <····@mouse-potato.com> wrote in message 
>> ···················@thalassa.informatimago.com...
>>
>>>Kenny Tilton <·············@nyc.rr.com> writes:
>>>>Damn the torpedos. Real programmers do not validate their input.
>>>
>>>Well, not exactly.  Real lisp programmers rely on the lisp functions
>>>to validate their input, and accept their behavior in border cases as
>>>useful:
>>
>> Damn.  Either way you slice it, I'm screwed!
>>
>>>[25]> (divide-in-pairs 'toto)
>>>*** - FIRST: TOTO is not a list
>>>[29]> (divide-in-pairs '(a b c))
>>>((A B) (C NIL))
>>>[31]> (divide-in-pairs '(a b c . d))
>>>*** - SECOND: D is not a list
>>
>> I actually agree those are reasonable behaviours for a general purpose 
>> utility.
>>
>> I guess I was coding as if there was some underlying specific purpose as 
>> it seems a rather arbitrary task.  So maybe in the real app it would be
>>
>> (defun assemble-grapple-gromets (new-parts-delivery)
>>   (cond ((not (listp new-parts-delivery)) (error "not a bag of parts"))
>>         ((null new-parts-delivery) nil)
>>         ((null (cdr new-parts-delivery))  (error "missing a falangee"))
>>         ((null (cddr new-parts-delivery)) (list new-parts-delivery))
>>         (t (cons (list (first new-parts-delivery) (second 
>> new-parts-delivery))
>>                  (assemble-grapple-gromets (cddr new-parts-delivery))))))
>>
>> Though that still won't make Kenny happy ;)
>
> Actually, validating input is fine if one wants to spend a solid fraction 
> of time typing in validation, and maintaining it as one refactors. It just 
> reminds me of the classic Lisp trade-off of guaranteed correctness vs 
> programmer productivity, as when we sniff at strong static typing. And the 
> punch line is the same: the same QA can be achieved without the strait 
> jackets, methinks.

Yes, you're right, IMO.

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Pascal Bourguignon
Subject: Re: newbie question
Date: 
Message-ID: <87u0cmq20c.fsf@thalassa.informatimago.com>
"Coby Beck" <·····@mercury.bc.ca> writes:

> I guess I was coding as if there was some underlying specific purpose as it 
> seems a rather arbitrary task.  So maybe in the real app it would be
>
> (defun assemble-grapple-gromets (new-parts-delivery)
>   (cond ((not (listp new-parts-delivery)) (error "not a bag of parts"))
>         ((null new-parts-delivery) nil)
>         ((null (cdr new-parts-delivery))  (error "missing a falangee"))
>         ((null (cddr new-parts-delivery)) (list new-parts-delivery))
>         (t (cons (list (first new-parts-delivery) (second 
> new-parts-delivery))
>                  (assemble-grapple-gromets (cddr new-parts-delivery))))))
>
> Though that still won't make Kenny happy ;)

My bet is that in a read application, instead of mere lists you'd have
CLOS objects, and they'd be able to report significant error messages
themselves.

But first, notice how the error messages provided by random lisp
implementations are more meaningfull than yours:


> [25]> (divide-in-pairs 'toto)
> *** - FIRST: TOTO is not a list

;; Good, I should put a list instead of TOTO. Let's try:

> [31]> (divide-in-pairs '(a b c . d))
> *** - SECOND: D is not a list

;; Ah ah, this mustn't be a dotted list.



[23]> (ASSEMBLE-GRAPPLE-GROMETS 'toto)

*** - not a bag of parts

;; What is not a bag of parts?  What can I do about it?


The following restarts are available:
ABORT          :R1      ABORT
Break 1 [24]> :q
[25]> (ASSEMBLE-GRAPPLE-GROMETS '(toto))

*** - missing a falangee

;; Oh yes?  Where?  How do I put a falangee?

The following restarts are available:
ABORT          :R1      ABORT
Break 1 [26]> 



So it might be a good idea to provide more meaningfull error messages,
but if  you lose information in  trying to do it,  you're just writing
a Microsoft program.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

THIS IS A 100% MATTER PRODUCT: In the unlikely event that this
merchandise should contact antimatter in any form, a catastrophic
explosion will result.
From: Coby Beck
Subject: Re: newbie question
Date: 
Message-ID: <6Neuf.24272$AP5.10791@edtnps84>
"Pascal Bourguignon" <····@mouse-potato.com> wrote in message 
···················@thalassa.informatimago.com...
> "Coby Beck" <·····@mercury.bc.ca> writes:
>
>> I guess I was coding as if there was some underlying specific purpose as 
>> it
>> seems a rather arbitrary task.  So maybe in the real app it would be
>>
>> (defun assemble-grapple-gromets (new-parts-delivery)
>>   (cond ((not (listp new-parts-delivery)) (error "not a bag of parts"))
>>         ((null new-parts-delivery) nil)
>>         ((null (cdr new-parts-delivery))  (error "missing a falangee"))
>>         ((null (cddr new-parts-delivery)) (list new-parts-delivery))
>>         (t (cons (list (first new-parts-delivery) (second
>> new-parts-delivery))
>>                  (assemble-grapple-gromets (cddr new-parts-delivery))))))
>>
>> Though that still won't make Kenny happy ;)
>
> My bet is that in a read application, instead of mere lists you'd have
> CLOS objects, and they'd be able to report significant error messages
> themselves.

Sure.  And I would raise application specific errors, not just deliver a 
message.

> But first, notice how the error messages provided by random lisp
> implementations are more meaningfull than yours:
>
>
>> [25]> (divide-in-pairs 'toto)
>> *** - FIRST: TOTO is not a list
>
> ;; Good, I should put a list instead of TOTO. Let's try:
>
>> [31]> (divide-in-pairs '(a b c . d))
>> *** - SECOND: D is not a list
>
> ;; Ah ah, this mustn't be a dotted list.
>
>
>
> [23]> (ASSEMBLE-GRAPPLE-GROMETS 'toto)
>
> *** - not a bag of parts
>
> ;; What is not a bag of parts?  What can I do about it?

(error "~A is not a bag of parts" new-parts-delivery)

> The following restarts are available:
> ABORT          :R1      ABORT
> Break 1 [24]> :q
> [25]> (ASSEMBLE-GRAPPLE-GROMETS '(toto))
>
> *** - missing a falangee
>
> ;; Oh yes?  Where?  How do I put a falangee?

Well, this is where you would want to raise a specific condition and have an 
appropriate handler higher up the call chain that can requisition a spare 
falangee from the falangee bin.

> So it might be a good idea to provide more meaningfull error messages,
> but if  you lose information in  trying to do it,  you're just writing
> a Microsoft program.

Now *that's* just insulting ;-)

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: tichy
Subject: Re: newbie question
Date: 
Message-ID: <dp8pn3$lcr$1@nemesis.news.tpi.pl>
Coby Beck wrote:
>> but couldn't find a way to apply it to the entire list.
>> i tried a recursive function,but it didnt give the output i wanted.
> 
> Here is a recursive solution to add to the loops:
> 
> (defun divide-in-pairs (list)
>   (cond ((not (listp list)) (error "non-list argument"))
>         ((null list) nil)
>         ((null (cdr list))  (error "odd number of elements"))
>         ((null (cddr list)) (list list))
>         (t (cons (list (first list) (second list))
>                  (divide-in-pairs (cddr list))))))
> 

Imo, mine is better:

(defun divide-in-pairs (list)
   (labels ((rec (list result)
              (if list
                  (let ((next (cddr list)))
                    (rec next (cons (ldiff list next) result)))
                (nreverse result))))
     (rec list '())))

CL-USER> (divide-in-pairs '(a b))
((A B))

CL-USER> (divide-in-pairs '(a b c d))
((A B) (C D))

CL-USER> (divide-in-pairs '(a b c d e))
((A B) (C D) (E))

Regards, Szymon.
From: Frank Buss
Subject: Re: newbie question
Date: 
Message-ID: <rvjkko44n18g.17f0w5jiqdn07$.dlg@40tude.net>
dvlfrnd wrote:

> (a b c d e f ......)
> 
> how do i turn this into
> ((a b) (c d) (e f)....)

you could use loop for it, one of my favorite Lisp macros, like described
in the other postings.

> then i tried this
> 
> (defun pair-add-element (n lst)
>   (mapcar #'(lambda (x) (list n x)) lst))
> 
> if i could split the list into two,
> where the original is (a b c d e f)
> to
> (a c e) and (b d f)
> 
> i could mapcar these two new lists using pair-add-element function
> above
> and return this
> ((a b) (c d) (e f))

your pair-add-element pairs every elemnt of the list "lst" with one element
"n". I don't understand how this would help. But mapcar itself can be used
for this, becaue it accepts multiple lists:

(mapcar #'(lambda (x y) (list x y)) '(a b) '(1 2)) -> ((A 1) (B 2))

> 1- how do i split a list into two, taking even'th and odd'th elemtns
> into separate lists

I'm sure there are more elegant solutions with car, cdr and recursive
functions, but again some loop magic:

(defun partition (list)
  (loop for (i j) on list by (function cddr)
        finally (return (values odd even))
        collect i into odd
        collect j into even))

(defun partition-pairs (list)
  (multiple-value-bind (odd even) (partition list)
    (mapcar #'(lambda (x y) (list x y)) odd even)))

or not much longer:

(defun partition-pairs (list)
  (multiple-value-bind (odd even) (partition list)
    (loop for x in odd for y in even collect (list x y))))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Frank Buss
Subject: Re: newbie question
Date: 
Message-ID: <88u9saestjhn$.16eo0at7hgwkk.dlg@40tude.net>
dvlfrnd wrote:

> but couldn't find a way to apply it to the entire list.
> i tried a recursive function,but it didnt give the output i wanted.

First I thought my Lisp knowledge is not good enough for a simple recursive
function, but then I just wrote it down in 2 minutes (and simplified it in
another 2 minutes). Don't look at it, if you want to write your own :-)

http://paste.frubar.net/398

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: John
Subject: Re: newbie question
Date: 
Message-ID: <CpidnfWdhPWRDCreRVn-jA@comcast.com>
dvlfrnd wrote:

> hi there,
> here's a newbie question
> im not a programmer and lisp is the only language i've ever worked
> with,
> but somehow i love it and i wanna learn.
> i was giving myself exercises and came up with this,pardon my
> programming inefficency and please help.
> say there's a list of even length
> (a b c d e f ......)
> 
> how do i turn this into
> ((a b) (c d) (e f)....)
> 
> simply how do i couple the elements?
> 
> i first tried to write a function:
> 
> (defun couple-first-two (lst)
>   (list (first lst) (second lst)))
> 
> but couldn't find a way to apply it to the entire list.
> i tried a recursive function,but it didnt give the output i wanted.
> 
> then i tried this
> 
> (defun pair-add-element (n lst)
>   (mapcar #'(lambda (x) (list n x)) lst))
> 
> if i could split the list into two,
> where the original is (a b c d e f)
> to
> (a c e) and (b d f)
> 
> i could mapcar these two new lists using pair-add-element function
> above
> and return this
> ((a b) (c d) (e f))
> 
> so the i have two questions
> 
> 1- how do i split a list into two, taking even'th and odd'th elemtns
> into separate lists
> 2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
> (e f)) in an efficient,proper code
> 
> how's my approach?? is it too silly, i know i have tons to learn about
> programming, please show me a way to solve this, including the code and
> the approach,design etc...
> 
> thank you very much
> and happy new year


Maybe something like this might help:

(defun pair (lst)
  (if (null lst)
      lst
      (let ((one (first lst))
            (two (second lst)))
        (if (null two)
            lst
            (cons (list one two) (pair (cddr lst)))))))

happy new year
John
From: John
Subject: Re: newbie question
Date: 
Message-ID: <5ZqdnXmOzaY5ZCrenZ2dnUVZ_v-dnZ2d@comcast.com>
John wrote:

> dvlfrnd wrote:
> 
>> hi there,
>> here's a newbie question
>> im not a programmer and lisp is the only language i've ever worked
>> with,
>> but somehow i love it and i wanna learn.
>> i was giving myself exercises and came up with this,pardon my
>> programming inefficency and please help.
>> say there's a list of even length
>> (a b c d e f ......)
>> 
>> how do i turn this into
>> ((a b) (c d) (e f)....)
>> 
>> simply how do i couple the elements?
>> 
>> i first tried to write a function:
>> 
>> (defun couple-first-two (lst)
>>   (list (first lst) (second lst)))
>> 
>> but couldn't find a way to apply it to the entire list.
>> i tried a recursive function,but it didnt give the output i wanted.
>> 
>> then i tried this
>> 
>> (defun pair-add-element (n lst)
>>   (mapcar #'(lambda (x) (list n x)) lst))
>> 
>> if i could split the list into two,
>> where the original is (a b c d e f)
>> to
>> (a c e) and (b d f)
>> 
>> i could mapcar these two new lists using pair-add-element function
>> above
>> and return this
>> ((a b) (c d) (e f))
>> 
>> so the i have two questions
>> 
>> 1- how do i split a list into two, taking even'th and odd'th elemtns
>> into separate lists
>> 2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
>> (e f)) in an efficient,proper code
>> 
>> how's my approach?? is it too silly, i know i have tons to learn about
>> programming, please show me a way to solve this, including the code and
>> the approach,design etc...
>> 
>> thank you very much
>> and happy new year
> 
> 
> Maybe something like this might help:
> 
> (defun pair (lst)
>   (if (null lst)
>       lst
>       (let ((one (first lst))
>             (two (second lst)))
>         (if (null two)
>             lst
>             (cons (list one two) (pair (cddr lst)))))))
> 
> happy new year
> John

OK, revision 0.1 :)

(defun pair (lst)
  (if (not (listp lst))
      lst
      (let ((one (first lst))
            (two (second lst)))
        (if (null two)
            lst
            (cons (list one two) (pair (cddr lst)))))))

John
From: airfoyle
Subject: Re: newbie question
Date: 
Message-ID: <1136228043.140793.122810@g43g2000cwa.googlegroups.com>
> [John <····@yahoo.com]
> OK, revision 0.1 :)
>
> (defun pair (lst)
>   (if (not (listp lst))
>       lst
>       (let ((one (first lst))
>             (two (second lst)))
>         (if (null two)
>             lst
>             (cons (list one two) (pair (cddr lst)))))))

You're not that close to the right answer:

(pair '(1 2 3 4 t nil oops e daisy))

((1 2) (3 4) t nil oops e daisy)

      -- Drew McDermott
         Yale CS Department
From: John
Subject: Re: newbie question
Date: 
Message-ID: <7YCdnU5JUpcdHSTeRVn-tA@comcast.com>
airfoyle wrote:

>> [John <····@yahoo.com]
>> OK, revision 0.1 :)
>>
>> (defun pair (lst)
>>   (if (not (listp lst))
>>       lst
>>       (let ((one (first lst))
>>             (two (second lst)))
>>         (if (null two)
>>             lst
>>             (cons (list one two) (pair (cddr lst)))))))
> 
> You're not that close to the right answer:
> 
> (pair '(1 2 3 4 t nil oops e daisy))
> 
> ((1 2) (3 4) t nil oops e daisy)
> 
>       -- Drew McDermott
>          Yale CS Department

:)

my original version tends to do better (but not much):

(defun pair (lst)
  (if (atom lst)
      lst
      (let ((one (first lst))
            (two (second lst)))
        (cons (list one two) (pair (cddr lst))))))

(print (pair nil))
(print (pair '(1)))
(print (pair '(1 2)))
(print (pair '(1 2 3)))
(print (pair '(1 2 3 4)))
(print (pair '(1 2 3 4 5)))
(print (pair 'toto))
(print (pair #'pair))
(print (pair '(1 2 3 4 t nil oops e daisy)))

NIL
((1 NIL))
((1 2))
((1 2) (3 NIL))
((1 2) (3 4))
((1 2) (3 4) (5 NIL))
TOTO
#<CLOSURE PAIR (LST) (DECLARE (SYSTEM::IN-DEFUN PAIR))
  (BLOCK PAIR
   (IF (ATOM LST) LST
    (LET ((ONE (FIRST LST)) (TWO (SECOND LST))) (CONS (LIST ONE TWO) (PAIR
(CDDR LST))))))>
((1 2) (3 4) (T NIL) (OOPS E) (DAISY NIL))
From: Eli Gottlieb
Subject: Re: newbie question
Date: 
Message-ID: <2CKtf.47814$XJ5.1340@twister.nyroc.rr.com>
dvlfrnd wrote:
> 2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
> (e f)) in an efficient,proper code
;Makes individual sub-lists out of pairs of list elements.
(defun pair-list (elements)
   (if (equalp (cadr elements) nil)
     nil
     (append
       (list (list (car elements) (cadr elements)))
       (pair-list (cddr elements)))))
;Gets the odd elements.
(defun get-odds (elements)
   (if (equalp (car elements) nil)
     nil
     (list (car elements) (get-odds (cddr elements)))))
;Gets the even elements.
(defun get-evens (elements)
   (if (equalp (cdr elements) nil)
     nil
     (list (cadr elements) (get-evens (cddr elements)))))
;Seperates the list into two lists of its odd and even elements, 
returning these new lists.
(defun seperate-odds-from-evens (elements)
   (list (get-odds elements) (get-evens elements)))
From: Eli Gottlieb
Subject: Re: newbie question
Date: 
Message-ID: <EHKtf.58556$XC4.21315@twister.nyroc.rr.com>
Eli Gottlieb wrote:
> dvlfrnd wrote:
> 
>> 2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
>> (e f)) in an efficient,proper code
> 
> ;Makes individual sub-lists out of pairs of list elements.
> (defun pair-list (elements)
>   (if (equalp (cadr elements) nil)
>     nil
>     (append
>       (list (list (car elements) (cadr elements)))
>       (pair-list (cddr elements)))))
> ;Gets the odd elements.
> (defun get-odds (elements)
>   (if (equalp (car elements) nil)
>     nil
>     (append (list (car elements)) (get-odds (cddr elements)))))
> ;Gets the even elements.
> (defun get-evens (elements)
>   (if (equalp (cdr elements) nil)
>     nil
>     (append (list (cadr elements)) (get-evens (cddr elements)))))
> ;Seperates the list into two lists of its odd and even elements, 
> returning these new lists.
> (defun seperate-odds-from-evens (elements)
>   (list (get-odds elements) (get-evens elements)))
Got get-evens and get-odds wrong.  Corrections in quote.
From: Coby Beck
Subject: Re: newbie question
Date: 
Message-ID: <3_Wtf.19506$km.5657@edtnps89>
"Eli Gottlieb" <···········@gmail.com> wrote in message 
·························@twister.nyroc.rr.com...
> ;Makes individual sub-lists out of pairs of list elements.
> (defun pair-list (elements)
>   (if (equalp (cadr elements) nil)

this (equalp (cadr elements) nil) strikes me as verbose to the point of 
being obfuscated.  I would recommend just using (null (cadr elements)) or 
even better just use (cadr elements) and reverse the order of your IF 
consequents.

>     nil
>     (append
>       (list (list (car elements) (cadr elements)))
>       (pair-list (cddr elements)))))

IMHO :)
-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Eli Gottlieb
Subject: Re: newbie question
Date: 
Message-ID: <XQYtf.63363$XC4.28754@twister.nyroc.rr.com>
Coby Beck wrote:
> "Eli Gottlieb" <···········@gmail.com> wrote in message 
> ·························@twister.nyroc.rr.com...
> 
>>;Makes individual sub-lists out of pairs of list elements.
>>(defun pair-list (elements)
>>  (if (equalp (cadr elements) nil)
> 
> 
> this (equalp (cadr elements) nil) strikes me as verbose to the point of 
> being obfuscated.  I would recommend just using (null (cadr elements)) or 
> even better just use (cadr elements) and reverse the order of your IF 
> consequents.
> 
> 
>>    nil
>>    (append
>>      (list (list (car elements) (cadr elements)))
>>      (pair-list (cddr elements)))))
> 
> 
> IMHO :)
I'm new, I'm still picking up these things.
From: David Sletten
Subject: Re: newbie question
Date: 
Message-ID: <anLtf.8675$ka.5431@tornado.socal.rr.com>
dvlfrnd wrote:

> say there's a list of even length
> (a b c d e f ......)
> 
> how do i turn this into
> ((a b) (c d) (e f)....)
> 
> simply how do i couple the elements?
> 

The LOOP versions that others have submitted are slicker, but here's a 
simple recursive version (assuming your condition that we are given a 
list of even length--otherwise you need some error checking):
(defun pair-up (l)
  (if (endp l)
      '()
      (destructuring-bind (a b . rest) l
        (cons (list a b) (pair-up rest)))) ))

Aloha,
David Sletten
From: Coby Beck
Subject: Re: newbie question
Date: 
Message-ID: <_xWtf.19503$km.18141@edtnps89>
"David Sletten" <·····@slytobias.com> wrote in message 
·······················@tornado.socal.rr.com...
> (defun pair-up (l)
>  (if (endp l)
>      '()
>      (destructuring-bind (a b . rest) l
>        (cons (list a b) (pair-up rest)))) ))

I like that use of destructuring-bind!

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Joerg Hoehle
Subject: Re: newbie question
Date: 
Message-ID: <uk6delx5t.fsf@users.sourceforge.net>
"Coby Beck" <·····@mercury.bc.ca> writes:
> "David Sletten" <·····@slytobias.com> wrote in message 
> >      (destructuring-bind (a b . rest) l
> I like that use of destructuring-bind!

Why not recommend (a b &rest rest) instead, which supersedes the IMHO
old-fashioned dot-list destructuring?

BTW, digging once again into CLHS, this time on lambda lists, I found
that the following pattern ought to be supported, which is news to me:

	&optional with destructuring template!

(destructuring-bind (a &optional ((b c) '(1 2))) '(x) (list* a b c))
-> (X 1 . 2)
(destructuring-bind (a &optional ((b c) '(1 2))) '(x (y z)) (list* a b c))
-> (X Y . Z)

It works at least in CLISP-2.35 and SBCL-0.9.xy on Linux.

Extended lambda lists are hairy indeed! :-)

Regards,
	Jorg Hohle
Telekom/T-Systems Technology Center
From: Lars Brinkhoff
Subject: Re: newbie question
Date: 
Message-ID: <85k6de1hcg.fsf@junk.nocrew.org>
Joerg Hoehle <······@users.sourceforge.net> writes:
> BTW, digging once again into CLHS, this time on lambda lists, I found
> that the following pattern ought to be supported, which is news to me:
>
> 	&optional with destructuring template!
> 
> Extended lambda lists are hairy indeed! :-)

Don't forget destructuring with &key, which is harier still.
From: Brian Downing
Subject: Re: newbie question
Date: 
Message-ID: <Al1uf.453269$084.267781@attbi_s22>
In article <··················@tornado.socal.rr.com>,
David Sletten  <·····@slytobias.com> wrote:
> The LOOP versions that others have submitted are slicker, but here's a 
> simple recursive version (assuming your condition that we are given a 
> list of even length--otherwise you need some error checking):

Why?  DESTRUCTURING-BIND seems to provide all the error checking you
need.

> (defun pair-up (l)
>   (if (endp l)
>       '()
>       (destructuring-bind (a b . rest) l
>         (cons (list a b) (pair-up rest)))) ))

-bcd
-- 
*** Brian Downing <bdowning at lavos dot net> 
From: Pascal Bourguignon
Subject: Re: newbie question
Date: 
Message-ID: <87zmmgz87s.fsf@thalassa.informatimago.com>
"dvlfrnd" <·······@yahoo.com> writes:
> 1- how do i split a list into two, taking even'th and odd'th elemtns
> into separate lists

You don't need that to solve the stated problem.

Recursively:

(defun odds  (l) (when l (cons (cadr l) (odds  (cddr l)))))
(defun evens (l) (when l (cons (car  l) (evens (cddr l)))))

This is not "efficient" since it'll use O(N) of stack space.


(defun odds (l &optional (r nil))
   (if l
      (odds (cddr l) (cons (cadr l) r))
      r))

(defun evens (l &optional (r nil))
   (if l
      (evens (cddr l) (cons (car l) r))
      r))

This could be "efficient", but Common Lisp doesn't require tail call
elimination, so you really want an interative solution. See the other
post for how to do it with loop.


(defun odds (l)
   (remove-if (let ((oddp t))    (lambda (x) (setf oddp  (not oddp))) l)))

(defun evens (l)
   (remove-if (let ((evenp nil)) (lambda (x) (setf evenp (not evenp))) l)))


> 2- how do i achieve this final goal of (a b c d e f) --->> ((a b) (c d)
> (e f)) in an efficient,proper code

You can do the same as above.

Here is another solution:

(defun pair-them (l)
  (mapcan (let ((skip nil)) (lambda (x y) (when (setf skip (not skip)) 
                                            (list (list x y))))) 
          l (cdr l)))

(pair-them '(a b c d e f g h)) --> ((A B) (C D) (E F) (G H))



But probably the more direct solutions are those using loop.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: Ivan Boldyrev
Subject: Re: newbie question
Date: 
Message-ID: <ruhk83-66v.ln1@ibhome.cgitftp.uiggm.nsc.ru>
On 9341 day of my life ·······@yahoo.com wrote:
> (defun couple-first-two (lst)
>  (list (first lst) (second lst)))

If you dislike LOOP:

(defun do-the-task (list)
  (let ((flip-flop t))
     (mapcon #'(lambda (rest)
                   (setf flip-flop (not flip-flop))
                   (if flip-flop
                       nil
                       (list (couple-first-two rest))))
             list)))

MAPCON applies function to sublist and catenates (with NCONC) returned
lists.  That's why NIL or (LIST ...) are returned, not just elements. 

-- 
Ivan Boldyrev

                        Today is the first day of the rest of your life.
From: Fred Gilham
Subject: Re: newbie question
Date: 
Message-ID: <u7irszs6ho.fsf@snapdragon.csl.sri.com>
Here's another flip-flop kind of solution, this one recursive, where
the arguments flip flop in the disassembly phase, and where the
disassembly and assembly phases are separated.

(defun pair-list (list &optional reds blues result)
  (if list
      (pair-list (cdr list)
		 blues 
		 (cons (car list) reds)
		 result)
      (if (or reds blues)
	  (pair-list nil
		     (cdr reds)
		     (cdr blues)
		     (cons (list (car reds) (car blues)) result))
	  result)))

-- 
Fred Gilham                                         ······@csl.sri.com
It is also well to note that there are probably not ten hand-grenades
in the entire philosophical community.                  -- John Lange
From: tichy
Subject: Re: newbie question
Date: 
Message-ID: <dp8rjc$ok9$1@atlantis.news.tpi.pl>
Hi.

Obscure solution (for fun...), it uses functions defined in series package
http://series.sourceforge.net

(defun blah (input fn)
   (collect
    (map-fn t (lambda (rest) (ldiff rest (funcall fn rest)))
            (scan-fn t (constantly input) fn #'endp))))


CL-USER> (blah '(a b c d e f g h) #'cdr)
((A) (B) (C) (D) (E) (F) (G) (H))

CL-USER> (blah '(a b c d e f g h) #'cddr)
((A B) (C D) (E F) (G H))

CL-USER> (blah '(a b c d e f g h) #'cdddr)
((A B C) (D E F) (G H))

CL-USER> (blah '(a b c d e f g h) #'cddddr)
((A B C D) (E F G H))

Regards, Szymon.
From: Giorgos Keramidas
Subject: Re: newbie question
Date: 
Message-ID: <86lkxxwez5.fsf@flame.pc>
On 31 Dec 2005 12:04:23 -0800, "dvlfrnd" <·······@yahoo.com> wrote:
> i was giving myself exercises and came up with this,pardon my
> programming inefficency and please help.
> say there's a list of even length
> (a b c d e f ......)
>
> how do i turn this into
> ((a b) (c d) (e f)....)
>
> simply how do i couple the elements?

A bit of experimentation with (loop) seems easy:

    CL-USER(5): (loop for (a b) on '(1 2 3 4)
                      collect (list a b))
    ((1 2) (2 3) (3 4) (4 NIL))

Almost there.  We only need the pairs starting from 'even' zero-based
indexes.  This is fairly straightforward to write using loop again:

    CL-USER(6): (loop for (a b) on '(1 2 3 4)
                      for index from 0
                      when (evenp index)
                      collect (list a b))
    ((1 2) (3 4))
    CL-USER(7):

The nice trick here, which I think I learned by lurking here and reading
the posts of Pascal Costanza, is that loop can have many "for" clauses,
which iterate over different sequences independently of each other.

- Giorgos
From: Zach Beane
Subject: Re: newbie question
Date: 
Message-ID: <m3irt1e4tl.fsf@unnamed.xach.com>
Giorgos Keramidas <········@ceid.upatras.gr> writes:

> Almost there.  We only need the pairs starting from 'even' zero-based
> indexes.  This is fairly straightforward to write using loop again:
> 
>     CL-USER(6): (loop for (a b) on '(1 2 3 4)
>                       for index from 0
>                       when (evenp index)
>                       collect (list a b))
>     ((1 2) (3 4))
>     CL-USER(7):

As has already been posted, "loop for (a b) on list by #'cddr" is a
better way to do this.

Zach
From: Giorgos Keramidas
Subject: Re: newbie question
Date: 
Message-ID: <86vex1watb.fsf@flame.pc>
On 03 Jan 2006 09:58:30 -0500, Zach Beane <····@xach.com> wrote:
> Giorgos Keramidas <········@ceid.upatras.gr> writes:
>> Almost there.  We only need the pairs starting from 'even' zero-based
>> indexes.  This is fairly straightforward to write using loop again:
>>
>>     CL-USER(6): (loop for (a b) on '(1 2 3 4)
>>                       for index from 0
>>                       when (evenp index)
>>                       collect (list a b))
>>     ((1 2) (3 4))
>>     CL-USER(7):
>
> As has already been posted, "loop for (a b) on list by #'cddr" is a
> better way to do this.

Sure.  I hadn't finished reading the entire thread when I posted this.
Which turns out a relatively bad idea, now that I've read everything :)
From: André Thieme
Subject: Re: newbie question
Date: 
Message-ID: <1136565858.692716.84950@z14g2000cwz.googlegroups.com>
(defun group (list n)
   (if (>= (length list) n)
       (cons (subseq list 0 n) (group (nthcdr n list) n))
       (and list (list list))))

It allows you to say even things like:
CL-USER> (group (list 10 20 30 40 50 60 70 80) 2)
((10 20) (30 40) (50 60) (70 80))
CL-USER> (group (list 10 20 30 40 50 60 70 80) 3)
((10 20 30) (40 50 60) (70 80))
CL-USER> (group (list 10 20 30 40 50 60 70 80) 4)
((10 20 30 40) (50 60 70 80))
...

It is not very efficient for very long lists because (>= (length list)
n) will take some time to run. You should write your own longer-than
function which returns T when list has at least n+1 elements. It
doesn't have to traverse the complete list.


André
--
From: dvlfrnd
Subject: Re: newbie question
Date: 
Message-ID: <1136773902.337197.3550@g47g2000cwa.googlegroups.com>
thank you very much all for your help, now i have lots of code to dig
into.
it helped me see various approaches,im keeping the page to refer to
later on my quests.
and special thanks to fred gilman who sincerely helped me,took his time
to explain the whole thing step by step..(would have sent a pm but it
fails everytime)
thank you all again
best
From: David Sletten
Subject: Re: newbie question
Date: 
Message-ID: <7Mpwf.37886$pE4.11768@tornado.socal.rr.com>
dvlfrnd wrote:
> thank you very much all for your help, now i have lots of code to dig
> into.
> it helped me see various approaches,im keeping the page to refer to
> later on my quests.
> and special thanks to fred gilman who sincerely helped me,took his time
> to explain the whole thing step by step..(would have sent a pm but it
> fails everytime)
> thank you all again
> best
> 

Fred would probably feel more appreciated if you spelled his last name 
correctly. :)

Aloha,
David Sletten
From: dvlfrnd
Subject: Re: newbie question
Date: 
Message-ID: <1136803099.208623.254180@g49g2000cwa.googlegroups.com>
well this means i need more sleep than caffeine :)
i apologize from Fred Gilham for the misspelling 
and thanks again
From: Thomas A. Russ
Subject: Re: newbie question
Date: 
Message-ID: <ymi4q4dib3f.fsf@sevak.isi.edu>
"=?iso-8859-1?q?Andr=E9_Thieme?=" <······························@justmail.de> writes:


> It is not very efficient for very long lists because (>=3D (length list)
> n) will take some time to run. You should write your own longer-than
> function which returns T when list has at least n+1 elements. It
> doesn't have to traverse the complete list.

What about just using NTH-CDR ?  With a line comment of course ;)


(if (nthcdr 2 list)    ; Test for more than 2 elements.
   ...
   ...

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: André Thieme
Subject: Re: newbie question
Date: 
Message-ID: <1136922092.027018.27700@g49g2000cwa.googlegroups.com>
Thomas A. Russ schrieb:

> André Thieme <······························@justmail.de> writes:
>
>
> > It is not very efficient for very long lists because (>=3D (length list)
> > n) will take some time to run. You should write your own longer-than
> > function which returns T when list has at least n+1 elements. It
> > doesn't have to traverse the complete list.
>
> What about just using NTH-CDR ?  With a line comment of course ;)
>
>
> (if (nthcdr 2 list)    ; Test for more than 2 elements.

Neat idea, thanks!

(defun group2 (list n)
  (let ((sublist (nthcdr n list)))
    (if sublist
	(cons (subseq list 0 n) (group2 sublist n))
	(and list (list list)))))


I did define group1 as the function which I posted before (using
length) and did these tests:

CL-USER> (let ((list nil))
	   (dotimes (i 250000)
	     (push (random 10000) list))
	   (time (group1 list 24))
	   nil)
Evaluation took:
  9.895 seconds of real time
  8.371727 seconds of user run time
  0.037994 seconds of system run time
  0 page faults and
  4,177,888 bytes consed.
NIL


CL-USER> (let ((list nil))
	   (dotimes (i 250000)
	     (push (random 10000) list))
	   (time (group2 list 24))
	   nil)
Evaluation took:
  0.014 seconds of real time
  0.011998 seconds of user run time
  0.002 seconds of system run time
  0 page faults and
  4,169,728 bytes consed.
NIL


I ran each of them three times and took the fastest results.


André
--