Google Gruppi non supporta piรน i nuovi post o le nuove iscrizioni Usenet. I contenuti storici continuano a essere visibili.

howto make a list from an array?

2.551 visualizzazioni
Passa al primo messaggio da leggere

george...@gmail.com

da leggere,
12 ago 2007, 20:14:1212/08/07
a
[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

da leggere,
12 ago 2007, 20:34:2712/08/07
a
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

da leggere,
12 ago 2007, 20:39:2612/08/07
a

<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

da leggere,
12 ago 2007, 22:07:4112/08/07
a
"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

da leggere,
13 ago 2007, 03:21:5713/08/07
a
"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

da leggere,
13 ago 2007, 03:56:2613/08/07
a
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 nuovi messaggi