Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

howto make a list from an array?

2 547 vues
Accéder directement au premier message non lu

george...@gmail.com

non lue,
12 août 2007, 20:14:1212/08/2007
à
[sbcl common lisp]
If I have an array
(let ((a (make-array 4)))

how do I get a list of it's elements?

I can do:
(map 'list #'(lambda(x)x) a)
but that looks really hokey.

Barry Margolin

non lue,
12 août 2007, 20:34:2712/08/2007
à
In article <1186964052.0...@w3g2000hsg.googlegroups.com>,
george...@gmail.com wrote:

(coerce a 'list)

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Carl Taylor

non lue,
12 août 2007, 20:39:2612/08/2007
à

<george...@gmail.com> wrote in message
news:1186964052.0...@w3g2000hsg.googlegroups.com...


(defun coerce-array-to-list (in-array)
(map 'list
#'identity
(make-array (array-total-size in-array)
:element-type (array-element-type in-array)
:displaced-to in-array)))


CL-USER 6 >
(coerce-array-to-list #2A((1 2 3) (4 5 6) (7 8 9)))
(1 2 3 4 5 6 7 8 9)


clt

Kent M Pitman

non lue,
12 août 2007, 22:07:4112/08/2007
à
"Carl Taylor" <carlt...@att.net> writes:

> CL-USER 6 >
> (coerce-array-to-list #2A((1 2 3) (4 5 6) (7 8 9)))
> (1 2 3 4 5 6 7 8 9)

Maclisp called this function LISTARRAY.

Espen Vestre

non lue,
13 août 2007, 03:21:5713/08/2007
à
"Carl Taylor" <carlt...@att.net> writes:

> (defun coerce-array-to-list (in-array)
> (map 'list
> #'identity
> (make-array (array-total-size in-array)
> :element-type (array-element-type in-array)
> :displaced-to in-array)))

If the OP only needed a function for 1-dimensional arrays, I highly
prefer Barry's solution (use COERCE!), but for a general array, the
following will probably be faster in most implementations, since it
will cons less:

(defun coerce-array-to-list (in-array)
(loop for i below (array-total-size in-array)
collect (row-major-aref in-array i)))
--
(espen)

Thomas F. Burdick

non lue,
13 août 2007, 03:56:2613/08/2007
à
On Aug 13, 4:07 am, Kent M Pitman <pit...@nhplace.com> wrote:

> "Carl Taylor" <carltay...@att.net> writes:
> > CL-USER 6 >
> > (coerce-array-to-list #2A((1 2 3) (4 5 6) (7 8 9)))
> > (1 2 3 4 5 6 7 8 9)
>
> Maclisp called this function LISTARRAY.

I'm surprised that someone would write that to return a flat list,
rather than treating the array as a matrix. Ie, I'd have expected:

((1 2 3) (4 5 6) (7 8 9))

I guess that's an argument for keeping it non-standard :-)


0 nouveau message