From: ············@gmail.com
Subject: howto make a list from an array?
Date: 
Message-ID: <1186964052.022689.284850@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.

From: Barry Margolin
Subject: Re: howto make a list from an array?
Date: 
Message-ID: <barmar-CD86AD.20342712082007@comcast.dca.giganews.com>
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 ***
From: Carl Taylor
Subject: Re: howto make a list from an array?
Date: 
Message-ID: <2LNvi.32187$ax1.14253@bgtnsc05-news.ops.worldnet.att.net>
<············@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>
"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.
From: Thomas F. Burdick
Subject: Re: howto make a list from an array?
Date: 
Message-ID: <1186991786.696121.124420@k79g2000hse.googlegroups.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 :-)
From: Espen Vestre
Subject: Re: howto make a list from an array?
Date: 
Message-ID: <m1bqdbx5ka.fsf@gazonk.vestre.net>
"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)