Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove the first element from a vector

25 views
Skip to first unread message

Cristina Ghirlanda

unread,
Dec 27, 2002, 2:31:52 PM12/27/02
to
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

Peter Seibel

unread,
Dec 27, 2002, 2:46:57 PM12/27/02
to
Cristina Ghirlanda <cri...@NOSPAM.interfree-DOT-it> writes:

I'm no expert but it seems like you can just do:

(defun remove-first-from-vector (vector)
(subseq vector 1))

-Peter

--
Peter Seibel
pe...@javamonkey.com

Cristina Ghirlanda

unread,
Dec 27, 2002, 2:58:41 PM12/27/02
to

> 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

Barry Margolin

unread,
Dec 27, 2002, 3:02:10 PM12/27/02
to
In article <aui9kv$kvj$1...@newsreader.mailgate.org>,

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, bar...@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.

Gabe Garza

unread,
Dec 27, 2002, 3:17:27 PM12/27/02
to
Cristina Ghirlanda <cri...@NOSPAM.interfree-DOT-it> writes:

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

Pascal Costanza

unread,
Dec 27, 2002, 3:58:49 PM12/27/02
to
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

Pascal Bourguignon

unread,
Dec 27, 2002, 10:13:52 PM12/27/02
to
Cristina Ghirlanda <cri...@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

Daniel Barlow

unread,
Dec 29, 2002, 9:30:14 PM12/29/02
to
Gabe Garza <g_g...@ix.netcom.com> writes:

> Cristina Ghirlanda <cri...@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

0 new messages