Hi,
I wrote a function that removes the first element from a vector, but
I think it's not the typical way people write in lisp.
(defun remove-first-from-vector (vector)
(let (new-vector)
(do ((index (1- (array-dimension vector 0)) (1- index)))
((= index 0) (concatenate 'vector new-vector))
(push (aref vector index) new-vector))))
I would like to hear some advice on how I can write it in a
more lispy way. Thanks.
Cristina
Cristina Ghirlanda <······@NOSPAM.interfree-DOT-it> writes:
> Hi,
>
> I wrote a function that removes the first element from a vector, but
> I think it's not the typical way people write in lisp.
>
> (defun remove-first-from-vector (vector)
> (let (new-vector)
> (do ((index (1- (array-dimension vector 0)) (1- index)))
> ((= index 0) (concatenate 'vector new-vector))
> (push (aref vector index) new-vector))))
>
> I would like to hear some advice on how I can write it in a
> more lispy way. Thanks.
I'm no expert but it seems like you can just do:
(defun remove-first-from-vector (vector)
(subseq vector 1))
-Peter
--
Peter Seibel
·····@javamonkey.com
> I'm no expert but it seems like you can just do:
> (defun remove-first-from-vector (vector)
> (subseq vector 1))
> -Peter
Thanks, I didn't remember at all that vector is a sequence.
Cristina
In article <············@newsreader.mailgate.org>,
Cristina Ghirlanda <······@NOSPAM.interfree-DOT-it> wrote:
>Hi,
>
> I wrote a function that removes the first element from a vector, but
> I think it's not the typical way people write in lisp.
>
>(defun remove-first-from-vector (vector)
> (let (new-vector)
> (do ((index (1- (array-dimension vector 0)) (1- index)))
> ((= index 0) (concatenate 'vector new-vector))
> (push (aref vector index) new-vector))))
>
> I would like to hear some advice on how I can write it in a
> more lispy way. Thanks.
Here are a couple of obvious ways.
(defun remove-first-from-vector (vector)
(subseq vector 1))
(defun remove-first-from-vector (vector)
(let ((new-size (1- (array-dimension vector 0)))
(new-vector (make-array new-size
:element-type (array-element-type vector))))
(dotimes (i new-size)
(setf (aref new-vector i) (aref vector (1+ i))))
new-vector))
--
Barry Margolin, ······@genuity.net
Genuity, Woburn, 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.
Cristina Ghirlanda <······@NOSPAM.interfree-DOT-it> writes:
> Hi,
>
> I wrote a function that removes the first element from a vector, but
> I think it's not the typical way people write in lisp.
>
> (defun remove-first-from-vector (vector)
> (let (new-vector)
> (do ((index (1- (array-dimension vector 0)) (1- index)))
> ((= index 0) (concatenate 'vector new-vector))
> (push (aref vector index) new-vector))))
>
> I would like to hear some advice on how I can write it in a
> more lispy way. Thanks.
I'd write it like this:
(defun remove-first-from-vector (vector)
(subseq vector 1))
And if SUBSEQ didn't exist, I'd write it like this:
(defun remove-first-from-vector (vector)
(let ((new-vector (make-array
(list (1- (length vector)))
:element-type (array-element-type vector))))
(loop for index from 1 below (length vector)
do (setf (aref new-vector (1- index)) (aref vector index)))
new-vector))
Gabe Garza
Gabe Garza <·······@ix.netcom.com> writes:
> Cristina Ghirlanda <······@NOSPAM.interfree-DOT-it> writes:
>> I wrote a function that removes the first element from a vector, but
>> I think it's not the typical way people write in lisp.
[...]
> I'd write it like this:
>
> (defun remove-first-from-vector (vector)
> (subseq vector 1))
>
> And if SUBSEQ didn't exist, I'd write it like this:
[...]
You don't specify whether a destructive implementation is allowed (or
desired). If so, you could also use REPLACE
(defun remove-first-from-vector (vector)
(replace vector vector :start2 1))
-dan
--
http://www.cliki.net/ - Link farm for free CL-on-Unix resources
Hi Cristina,
Try the function subseq - it can be found in chapter 17 on sequences in
the HyperSpec, or chapter 14 in cltl2.
All the best,
Pascal
Cristina Ghirlanda wrote:
> Hi,
>
> I wrote a function that removes the first element from a vector, but
> I think it's not the typical way people write in lisp.
[...]
--
Given any rule, however �fundamental� or �necessary� for science, there
are always circumstances when it is advisable not only to ignore the
rule, but to adopt its opposite. - Paul Feyerabend
Cristina Ghirlanda <······@NOSPAM.interfree-DOT-it> writes:
> Hi,
>
> I wrote a function that removes the first element from a vector, but
> I think it's not the typical way people write in lisp.
>
> (defun remove-first-from-vector (vector)
And remember that if you have to remove a lot of first elements from
vector, it may be more efficient to use a list instead and use CDR.
--
__Pascal_Bourguignon__ http://www.informatimago.com/
----------------------------------------------------------------------
There is a fault in reality. do not adjust your minds. -- Salman Rushdie