From: ········@hotmail.com
Subject: Converting between lists and vectors
Date: 
Message-ID: <83mbrg$krn$1@nnrp1.deja.com>
Hi,

I've implemented conversion between lists and vectors by using defun,
but the code is much too rococo.  Is there a simple way to do this with
macros?  I tried

(defmacro list-to-vector (lst)
  `(vector ,@lst))

(let ((lst '(1 2 3))) (list-to-vector lst)) signals an error,
but (list-to-vector (1 2 3)) => #(1 2 3)

The second (working) expansion is bad-ugly, and not what I'm looking
for at all.

So close!  How can I modify the macro to achieve the desired results?
Are macros not the way to go?  Is there a simple, elegant solution to
conversions here?  If it helps any, after I convert, I no longer care
about the original list.

Sincerely,
Douglas M. Auclair


Sent via Deja.com http://www.deja.com/
Before you buy.

From: Barry Margolin
Subject: Re: Converting between lists and vectors
Date: 
Message-ID: <OWy74.34$fk1.1143@burlma1-snr2>
In article <············@nnrp1.deja.com>,  <········@hotmail.com> wrote:
>Hi,
>
>I've implemented conversion between lists and vectors by using defun,
>but the code is much too rococo.  Is there a simple way to do this with
>macros?  I tried
>
>(defmacro list-to-vector (lst)
>  `(vector ,@lst))
>
>(let ((lst '(1 2 3))) (list-to-vector lst)) signals an error,
>but (list-to-vector (1 2 3)) => #(1 2 3)
>
>The second (working) expansion is bad-ugly, and not what I'm looking
>for at all.
>
>So close!  How can I modify the macro to achieve the desired results?
>Are macros not the way to go?  Is there a simple, elegant solution to
>conversions here?  If it helps any, after I convert, I no longer care
>about the original list.

A macro is not the way to go, since it can only work with data that's
available at macro expansion time, not runtime variables.

What's wrong with:

(defun list-to-vector (list)
  (make-array (length list) :initial-elements list))

That's not very rococo.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Roger Corman
Subject: Re: Converting between lists and vectors
Date: 
Message-ID: <385ed4ea.348362779@nntp.best.com>
On Mon, 20 Dec 1999 23:26:38 GMT, Barry Margolin
<······@bbnplanet.com> wrote:

>In article <············@nnrp1.deja.com>,  <········@hotmail.com> wrote:
>>Hi,
>>
>>I've implemented conversion between lists and vectors by using defun,
>>but the code is much too rococo.  Is there a simple way to do this with
>>macros?  I tried
>>
>>(defmacro list-to-vector (lst)
>>  `(vector ,@lst))
>>
>>(let ((lst '(1 2 3))) (list-to-vector lst)) signals an error,
>>but (list-to-vector (1 2 3)) => #(1 2 3)
>>
>What's wrong with:
>
>(defun list-to-vector (list)
>  (make-array (length list) :initial-elements list))
>
I know, I know...  :-)
I am sure you meant:

(make-array (length list) :initial-contents list)

Other good options:

(coerce list 'vector)
(concatenate 'vector list)

etc.


Roger
From: ········@hotmail.com
Subject: Re: Converting between lists and vectors
Date: 
Message-ID: <83ojtt$6q6$1@nnrp1.deja.com>
>
> (defun list-to-vector (list)
>   (make-array (length list) :initial-contents list))
>
> That's not very rococo.

You're quite right!  And, the hyperspec has an example that does just
that.  Sometimes, one does not see something right in front of one's
nose.  Thanks for the help.

Sincerely,
Doug Auclair


Sent via Deja.com http://www.deja.com/
Before you buy.