Hi,
I've figured out how to copy the value of a list into an array and
vice-versa using a 'equals' function (defined below) and the map
function.
My question is, is there a single LISP function which does this
without using a second function like 'equals'? I've been looking
through my XLISP2.1e manual but haven't found it so far.
Thanks
David
T
> (defun equals (x)
(* x 1.0))
EQUALS
> (setf my-list (list 1 2 3 4 5))
(1 2 3 4 5)
> (setf my-array (map 'array 'equals my-list))
#(1 2 3 4 5)
> (dribble)
"David R. Sky" <···@viper.wapvi.bc.ca> writes:
> Hi,
>
> I've figured out how to copy the value of a list into an array and
> vice-versa using a 'equals' function (defined below) and the map
> function.
>
> My question is, is there a single LISP function which does this
> without using a second function like 'equals'? I've been looking
> through my XLISP2.1e manual but haven't found it so far.
>
> Thanks
>
> David
>
> T
> > (defun equals (x)
> (* x 1.0))
> EQUALS
> > (setf my-list (list 1 2 3 4 5))
> (1 2 3 4 5)
> > (setf my-array (map 'array 'equals my-list))
> #(1 2 3 4 5)
> > (dribble)
There's no single function which takes a single list argument and
returns a vector of its contents. However, writing such a function is
a one-liner, which is probably why it doesn't exist. Among your
options for that one line are:
(map 'vector #'identity list)
(coerce list 'vector)
(apply #'vector list)
I'm speaking here of Common Lisp, so I don't know for sure for XLISP.
I suspect that at least one of those will work for you there.
Thanks Thomas, see my response to Frank's own response just now. David
On Sun, 23 Apr 2006, Thomas F. Burdick wrote:
> "David R. Sky" <···@viper.wapvi.bc.ca> writes:
>
>> Hi,
>>
>> I've figured out how to copy the value of a list into an array and
>> vice-versa using a 'equals' function (defined below) and the map
>> function.
>>
>> My question is, is there a single LISP function which does this
>> without using a second function like 'equals'? I've been looking
>> through my XLISP2.1e manual but haven't found it so far.
>>
>> Thanks
>>
>> David
>>
>> T
>>> (defun equals (x)
>> (* x 1.0))
>> EQUALS
>>> (setf my-list (list 1 2 3 4 5))
>> (1 2 3 4 5)
>>> (setf my-array (map 'array 'equals my-list))
>> #(1 2 3 4 5)
>>> (dribble)
>
> There's no single function which takes a single list argument and
> returns a vector of its contents. However, writing such a function is
> a one-liner, which is probably why it doesn't exist. Among your
> options for that one line are:
>
> (map 'vector #'identity list)
> (coerce list 'vector)
> (apply #'vector list)
>
> I'm speaking here of Common Lisp, so I don't know for sure for XLISP.
> I suspect that at least one of those will work for you there.
>
>
David R. Sky wrote:
> I've figured out how to copy the value of a list into an array and
> vice-versa using a 'equals' function (defined below) and the map
> function.
In Common Lisp there is the identity function:
http://www.lisp.org/HyperSpec/Body/fun_identity.html
> My question is, is there a single LISP function which does this
> without using a second function like 'equals'? I've been looking
> through my XLISP2.1e manual but haven't found it so far.
I don't know, if XLISP support Common Lisp, you may download one of the
free Common Lisp implementations, but one solution:
(defun list->array (list)
(make-array (list (length list)) :initial-contents list))
CL-USER > (list->array '(1 2 3))
#(1 2 3)
(defun array->list (list)
(loop for i across list collect i))
CL-USER > (array->list #(1 2 3))
(1 2 3)
>> (defun equals (x)
> (* x 1.0))
> EQUALS
>> (setf my-list (list 1 2 3 4 5))
> (1 2 3 4 5)
>> (setf my-array (map 'array 'equals my-list))
> #(1 2 3 4 5)
This doesn't work in Common Lisp.
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Thanks Frank. The identity function is defined in the manual, but is an
"unbound function" when I try to run it.
It isn't "vital" that I have an identity function, I can simply use the map
function as I described previously but with a simpler "identity" function
than I wrote (defun value (x) x) . So I'd use the map function
(map 'array 'value list)
I'm using XLISP because I do a lot of programming in Nyquist, an offshoot of
XLISP which works with sound - I test some of my non-sound functions first
in XLISP
David
On Sun, 23 Apr 2006, Frank Buss wrote:
> David R. Sky wrote:
>
>> I've figured out how to copy the value of a list into an array and
>> vice-versa using a 'equals' function (defined below) and the map
>> function.
>
> In Common Lisp there is the identity function:
>
> http://www.lisp.org/HyperSpec/Body/fun_identity.html
>
>> My question is, is there a single LISP function which does this
>> without using a second function like 'equals'? I've been looking
>> through my XLISP2.1e manual but haven't found it so far.
>
> I don't know, if XLISP support Common Lisp, you may download one of the
> free Common Lisp implementations, but one solution:
>
> (defun list->array (list)
> (make-array (list (length list)) :initial-contents list))
>
> CL-USER > (list->array '(1 2 3))
> #(1 2 3)
>
> (defun array->list (list)
> (loop for i across list collect i))
>
> CL-USER > (array->list #(1 2 3))
> (1 2 3)
>
>>> (defun equals (x)
>> (* x 1.0))
>> EQUALS
>>> (setf my-list (list 1 2 3 4 5))
>> (1 2 3 4 5)
>>> (setf my-array (map 'array 'equals my-list))
>> #(1 2 3 4 5)
>
> This doesn't work in Common Lisp.
>
>
On Sun, 23 Apr 2006 03:52:51 -0700, David R. Sky wrote:
> Hi,
>
> I've figured out how to copy the value of a list into an array and
> vice-versa using a 'equals' function (defined below) and the map
> function.
>
> My question is, is there a single LISP function which does this
> without using a second function like 'equals'? I've been looking
> through my XLISP2.1e manual but haven't found it so far.
Hmm, I don't use XLISP but doesn't it have the identity function?
With CL you can do '(map 'vector #'identity my-list)'
[Note: CL's vector is an array of one dimension].
I don't understand your 'equals function - why the multiplication?
Doesn't this limit the type of the members of the list?
Why not: (defun equals (x) x)
[I'd rather name it identity]
HTH Ralf Mattes
> Thanks
>
> David
>
> T
>> (defun equals (x)
> (* x 1.0))
> EQUALS
>> (setf my-list (list 1 2 3 4 5))
> (1 2 3 4 5)
>> (setf my-array (map 'array 'equals my-list))
> #(1 2 3 4 5)
>> (dribble)
Thanks Ralf, see my response to Frank's post just now. David
On Sun, 23 Apr 2006, R. Mattes wrote:
> On Sun, 23 Apr 2006 03:52:51 -0700, David R. Sky wrote:
>
>> Hi,
>>
>> I've figured out how to copy the value of a list into an array and
>> vice-versa using a 'equals' function (defined below) and the map
>> function.
>>
>> My question is, is there a single LISP function which does this
>> without using a second function like 'equals'? I've been looking
>> through my XLISP2.1e manual but haven't found it so far.
>
> Hmm, I don't use XLISP but doesn't it have the identity function?
> With CL you can do '(map 'vector #'identity my-list)'
> [Note: CL's vector is an array of one dimension].
>
> I don't understand your 'equals function - why the multiplication?
> Doesn't this limit the type of the members of the list?
> Why not: (defun equals (x) x)
> [I'd rather name it identity]
>
> HTH Ralf Mattes
>
>> Thanks
>>
>> David
>>
>> T
>>> (defun equals (x)
>> (* x 1.0))
>> EQUALS
>>> (setf my-list (list 1 2 3 4 5))
>> (1 2 3 4 5)
>>> (setf my-array (map 'array 'equals my-list))
>> #(1 2 3 4 5)
>>> (dribble)
>
>