From: Martti Halminen
Subject: Re: how  to efficiently unflat a list
Date: 
Message-ID: <36E69449.2650@dpe.rm_spam_trap.fi>
kp gores wrote:

> i have the following problem:
> in a list (a b c d e) i have found by pattern-matching that the elements c
> d should be grouped together in a list whose car is f . as result i want
> (a b (f c d) e).
> 
> how do i efficiently implement this in lisp with minimum garbage?

This minimum garbage stuff points to an implementation splicing the
existing list structure with setf or rplacd. Slightly error-prone stuff,
so usually not recommended for beginners.

When playing with this, (setq *print-circle* T) might help...

Didn't bother writing a program, but here's something like that done by
hand:

D++(4): (setq ll (copy-list '(a b c d e)))
(A B C D E)

D++(6): (setq group (butlast (nthcdr 2 ll)))
(C D)

D++(10): (setf (cdr(nthcdr 2 ll)) (nthcdr 4 ll))
(E)
D++(11): ll
(A B C E)

D++(13): (setf (nth 2 ll) (cons 'f group))
(F C D)
D++(14): ll
(A B (F C D) E)

-- The previous is obviously done using fixed positions and list
lengths, you might need to make it more general.
-- That copy-list is just a reminder that using '(...) for creating
lists might create the same (eq) list every time used in some cases...



--