Re: Collecting like-labelled sublists of a list



Madhu <enometh@xxxxxxxx> writes:

* Steve Allan <uljzj2luv.fsf@xxxxxxxxxxxxxx> :
Wrote on Wed, 30 Jul 2008 12:49:44 -0700:
| I'm a junior lisper, so I tried a different approach mostly for my own
| education.
|
| (let ((mylist '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l)
| (4 m n) (2 o p) (4 q) (5 s t)))
| (ar (make-array 10 :adjustable t)))
| (mapcar (lambda (l)
| (setf (aref ar (first l))
| (append (aref ar (first l)) (rest l)))) mylist))
|
|
| My choice of 10 for the initial size of the array was arbitrary. I
| chose the non-destructive append over nconc, but nconc might be ok.

This is a correct approach. Note it is the almost identical to what
[the experienced] Kaz suggests in this thread! Like he says, it will
work well "If the labels are within a small numeric range, like 0 to 99"

[Even if the label bound is not known but is known to be small, as you
have declared AR adjustable, if the label I is greater than (LENGTH AR)
you can resize AR before setting the Ith element.]


I realize now that I don't understand how adjustable arrays work. I
assumed the expanding would happen automagically, but after a quick
test I see that it doesn't. So my approach doesn't work the way I had
intended. I'll have to read up on adjustable arrays a bit.

However in this case the label may not be useful to index the array, as
elsewhere in the thread Cortez has said:

Actually the function uses ASSOC instead of NTH, because the labels
themselves will not necessarily be integers

Ah, that does change things.

--
-- Steve
.