[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.
In article <························@w3g2000hsg.googlegroups.com>,
············@gmail.com wrote:
> [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.
(coerce a 'list)
--
Barry Margolin, ······@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 ***
<············@gmail.com> wrote in message
·····························@w3g2000hsg.googlegroups.com...
> [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.
(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
From: Kent M Pitman
Subject: Re: howto make a list from an array?
Date:
Message-ID: <ur6m8xk42.fsf@nhplace.com>
On Aug 13, 4:07 am, Kent M Pitman <······@nhplace.com> wrote:
> "Carl Taylor" <··········@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 :-)
"Carl Taylor" <··········@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)