From: Delaregue
Subject: list, objects, assoc?
Date: 
Message-ID: <6b4aa54a.0107110308.2bc39c93@posting.google.com>
Hello,

I've got a list of object ie: (A B C D ....)
This list can be very long.

I want to compute a value for each of theseobject. 
ie: computation based on A give me a number, 2004.23
B -> 134.2456
C -> 560.0113
etc...


I do not want to store indefinitely this value but I want to be able
to sort (A B C D) based on this number.

How can I do this?

Please note that this value is related to A and *all* the others
objects. As such, recursion on a smaller list doesn't seem to be
possible.

Delaregue

From: Christophe Rhodes
Subject: Re: list, objects, assoc?
Date: 
Message-ID: <sqd777lnhe.fsf@lambda.jesus.cam.ac.uk>
·········@netscape.net (Delaregue) writes:

> I've got a list of object ie: (A B C D ....)
> This list can be very long.
> 
> I want to compute a value for each of theseobject. 
> ie: computation based on A give me a number, 2004.23
> B -> 134.2456
> C -> 560.0113
> etc...
> 
> 
> I do not want to store indefinitely this value but I want to be able
> to sort (A B C D) based on this number.
> 
> How can I do this?

Well, until your next paragraph I would have said

(sort my-list #'< :key #'my-computation)

but ...
 
> Please note that this value is related to A and *all* the others
> objects. As such, recursion on a smaller list doesn't seem to be
> possible.

... now I don't understand what you mean.

Christophe
-- 
Jesus College, Cambridge, CB5 8BL                           +44 1223 510 299
http://www-jcsu.jesus.cam.ac.uk/~csr21/                  (defun pling-dollar 
(str schar arg) (first (last +))) (make-dispatch-macro-character #\! t)
(set-dispatch-macro-character #\! #\$ #'pling-dollar)
From: BPT
Subject: Re: list, objects, assoc?
Date: 
Message-ID: <87y9ou80pk.fsf@lupus.i-did-not-set--mail-host-address--so-shoot-me>
Christophe Rhodes <·····@cam.ac.uk> writes:

> ·········@netscape.net (Delaregue) writes:
> 
> > I've got a list of object ie: (A B C D ....)
> > This list can be very long.
> > 
> > I want to compute a value for each of theseobject. 
> > ie: computation based on A give me a number, 2004.23
> > B -> 134.2456
> > C -> 560.0113
> > etc...
> > 
> > 
> > I do not want to store indefinitely this value but I want to be able
> > to sort (A B C D) based on this number.
> > 
> > How can I do this?
> 
> Well, until your next paragraph I would have said
> 
> (sort my-list #'< :key #'my-computation)
> 
> but ...
>  
> > Please note that this value is related to A and *all* the others
> > objects. As such, recursion on a smaller list doesn't seem to be
> > possible.
> 
> ... now I don't understand what you mean.
> 
AFAICT he means that he needs access  to the values of A, etc and thus
cannot just use simple recursion to solve his problem. I may be wrong,
though. I  don't know much CL  yet, but maybe he  wants something like
the `accumulate' function as explained in the book `Simply Scheme'.

> Christophe
> -- 
> Jesus College, Cambridge, CB5 8BL                           +44 1223 510 299
> http://www-jcsu.jesus.cam.ac.uk/~csr21/                  (defun pling-dollar 
> (str schar arg) (first (last +))) (make-dispatch-macro-character #\! t)
> (set-dispatch-macro-character #\! #\$ #'pling-dollar)

-- 
BPT <···@hobbiton.org>
backronym for Linux: Linux Is Not Unix
From: Christos Kloukinas *replace Xos with inria*
Subject: Re: list, objects, assoc?
Date: 
Message-ID: <m24rr07tlp.fsf@Xos.fr>
BPT <···@hobbiton.org> writes:

> Christophe Rhodes <·····@cam.ac.uk> writes:
> 
> > ·········@netscape.net (Delaregue) writes:
> > 
> > > I've got a list of object ie: (A B C D ....)
> > > This list can be very long.
> > > 
> > > I want to compute a value for each of theseobject. 
> > > ie: computation based on A give me a number, 2004.23
> > > B -> 134.2456
> > > C -> 560.0113
> > > etc...
> > > 
> > > 
> > > I do not want to store indefinitely this value but I want to be able
> > > to sort (A B C D) based on this number.
> > > 
> > > How can I do this?
> > 
> > Well, until your next paragraph I would have said
> > 
> > (sort my-list #'< :key #'my-computation)
> > 
> > but ...
> >  
> > > Please note that this value is related to A and *all* the others
> > > objects. As such, recursion on a smaller list doesn't seem to be
> > > possible.
> > 
> > ... now I don't understand what you mean.
> > 
Since maplist takes successive cdr's of a list, can't you use that
after you have used reverse?
I.e.
(let* ((my-list '(a b c d ...))
       (the-values (maplist #'my-computation (reverse my-list)))
       (new-list (mapcar #'cons my-list the-values)))
    (mapcar #'car (sort new-list #'< :key #'cdr)))

> AFAICT he means that he needs access  to the values of A, etc and thus
> cannot just use simple recursion to solve his problem. I may be wrong,
> though. I  don't know much CL  yet, but maybe he  wants something like
> the `accumulate' function as explained in the book `Simply Scheme'.
> 
> > Christophe
> BPT <···@hobbiton.org>
From: Frank A. Adrian
Subject: Re: list, objects, assoc?
Date: 
Message-ID: <1j937.941$ds1.504736@news.uswest.net>
If this list is very long, you'd probably be better off keeping it as an
array.  Not only would it take less space, it would also take less time to
access the elements you need for the computation.  If you get the data in a
list, use COERCE to turn it into an array (or just call MAKE_ARRAY, passing
the list as its :INITIAL-CONTENTS.  Compute the function, storing the CONS
of the objects and function values in the array.  Then use SORT on the
collection using the :KEY value #'CDR.  Loop through the array again,
storing the CAR of each cons cell.  Voila!  If you need it as a list
afterwards, use COERCE to turn it back into a LIST or in your last loop,
COLLECT the CARs.

Now, all of this is predicated on the idea that there really is NO simple
way to calculate the values without looking at all of the other values.  If
it only depends on the items after the object in the list, you can still
iterate down the list, collecting results from the bottom up.  Even so,
you're still probably better off using an array.

faa

"Delaregue" <·········@netscape.net> wrote in message
·································@posting.google.com...
> Hello,
>
> I've got a list of object ie: (A B C D ....)
> This list can be very long.
>
> I want to compute a value for each of theseobject.
> ie: computation based on A give me a number, 2004.23
> B -> 134.2456
> C -> 560.0113
> etc...
>
>
> I do not want to store indefinitely this value but I want to be able
> to sort (A B C D) based on this number.
>
> How can I do this?
>
> Please note that this value is related to A and *all* the others
> objects. As such, recursion on a smaller list doesn't seem to be
> possible.
>
> Delaregue
From: Delaregue
Subject: Re: list, objects, assoc?
Date: 
Message-ID: <6b4aa54a.0107120602.5671f470@posting.google.com>
"Frank A. Adrian" <·······@uswest.net> wrote in message news:<····················@news.uswest.net>...
> If this list is very long, you'd probably be better off keeping it as an
> array.  Not only would it take less space, it would also take less time to
> access the elements you need for the computation.  If you get the data in a
> list, use COERCE to turn it into an array (or just call MAKE_ARRAY, passing
> the list as its :INITIAL-CONTENTS.  Compute the function, storing the CONS
> of the objects and function values in the array.  Then use SORT on the
> collection using the :KEY value #'CDR.  Loop through the array again,
> storing the CAR of each cons cell.  Voila!  If you need it as a list
> afterwards, use COERCE to turn it back into a LIST or in your last loop,
> COLLECT the CARs.
> 
> Now, all of this is predicated on the idea that there really is NO simple
> way to calculate the values without looking at all of the other values.  If
> it only depends on the items after the object in the list, you can still
> iterate down the list, collecting results from the bottom up.  Even so,
> you're still probably better off using an array.
> 
> faa
> 
> "Delaregue" <·········@netscape.net> wrote in message
> ·································@posting.google.com...
> > Hello,
> >
> > I've got a list of object ie: (A B C D ....)
> > This list can be very long.
> >
> > I want to compute a value for each of theseobject.
> > ie: computation based on A give me a number, 2004.23
> > B -> 134.2456
> > C -> 560.0113
> > etc...
> >
> >
> > I do not want to store indefinitely this value but I want to be able
> > to sort (A B C D) based on this number.
> >
> > How can I do this?
> >
> > Please note that this value is related to A and *all* the others
> > objects. As such, recursion on a smaller list doesn't seem to be
> > possible.
> >
> > Delaregue
Works great. Thanks as I learn a few things( coerce and #'cdr).
Since all my functions work with list though, I have to make them work
with array as well...
From: Frank A. Adrian
Subject: Re: list, objects, assoc?
Date: 
Message-ID: <Wli37.273$hn6.149608@news.uswest.net>
> Works great. Thanks as I learn a few things( coerce and #'cdr).
> Since all my functions work with list though, I have to make them work
> with array as well...

It's not as hard as it looks.  Check out the sequence functions (Hyperspec
section 17).  If speed is not the most critical issue (true for about 90% of
all code), the generic sequence functions will work on both lists and
arrays.

faa