From: Cristina Ghirlanda
Subject: remove the first element from a vector
Date: 
Message-ID: <aui9kv$kvj$1@newsreader.mailgate.org>
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

From: Peter Seibel
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <m3lm2bqm6n.fsf@localhost.localdomain>
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
From: Cristina Ghirlanda
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <auib77$lqb$1@newsreader.mailgate.org>
> 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
From: Barry Margolin
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <6l2P9.11$fU2.321@paloalto-snr1.gtei.net>
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.
From: Gabe Garza
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <878yybtdm4.fsf@ix.netcom.com>
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      
From: Daniel Barlow
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <87bs346xxl.fsf@noetbook.telent.net>
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 
From: Pascal Costanza
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <auietv$26a$1@newsreader2.netcologne.de>
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
From: Pascal Bourguignon
Subject: Re: remove the first element from a vector
Date: 
Message-ID: <871y42omxb.fsf@thalassa.informatimago.com>
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