How to delete array element by its index?

  • Follow


I have an adjustable array :

> (setq foo  (make-array 0  :element-type 'fixnum
       :fill-pointer 0 :adjustable t))
; added some elements
> foo
#(1 2 1 3 4 1 1 5 101)


How to remove say third element?

Slobodan
0
Reply slobodan.blazeski (1459) 3/6/2010 8:24:30 PM

In article 
<b63161f5-8ec6-42f0-b489-469e6f3f0293@x22g2000yqx.googlegroups.com>,
 Slobodan Blazeski <slobodan.blazeski@gmail.com> wrote:

> I have an adjustable array :
> 
> > (setq foo  (make-array 0  :element-type 'fixnum
>        :fill-pointer 0 :adjustable t))
> ; added some elements
> > foo
> #(1 2 1 3 4 1 1 5 101)
> 
> 
> How to remove say third element?
> 
> Slobodan

This functionality is not built-in to CL.  You have to roll your own.  
Here's a general way to do it if you don't care much about efficiency:

(defun delete-nth (sequence n)
  (let ((g (gensym)))
    (setf (elt sequence n) g)
    (delete g sequence)))

A better way would be to manually slide all the downstream elements one 
position left and then adjust the size of the vector.

rg
0
Reply Ron 3/6/2010 8:43:18 PM


On Mar 6, 9:43=A0pm, Ron Garret <rNOSPA...@flownet.com> wrote:
> In article
> <b63161f5-8ec6-42f0-b489-469e6f3f0...@x22g2000yqx.googlegroups.com>,
> =A0Slobodan Blazeski <slobodan.blaze...@gmail.com> wrote:
>
> > I have an adjustable array :
>
> > > (setq foo =A0(make-array 0 =A0:element-type 'fixnum
> > =A0 =A0 =A0 =A0:fill-pointer 0 :adjustable t))
> > ; added some elements
> > > foo
> > #(1 2 1 3 4 1 1 5 101)
>
> > How to remove say third element?
>
> > Slobodan
>
> This functionality is not built-in to CL. =A0You have to roll your own. =
=A0
> Here's a general way to do it if you don't care much about efficiency:
>
> (defun delete-nth (sequence n)
> =A0 (let ((g (gensym)))
> =A0 =A0 (setf (elt sequence n) g)
> =A0 =A0 (delete g sequence)))
>
> A better way would be to manually slide all the downstream elements one
> position left and then adjust the size of the vector.
>
> rg
Ok will use below for now.

 (defun delete-at (i seq)
      (let ((slide (subseq seq (1+ i)))
	    (num (1- (fill-pointer seq))))
        (replace seq slide  :start1 i)
	(adjust-array seq num
		      :fill-pointer num)))


STYLE-WARNING: redefining DELETE-AT in DEFUN
; Evaluation aborted.
DELETE-AT
CL>
CL> (delete-at 2 foo)
#(0 1 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 9)
CL>  (defun delete-at (i seq)
      (let ((slide (subseq seq (1+ i)))
	    (num (1- (fill-pointer seq))))
        (replace seq slide  :start1 i)
	(adjust-array seq num
		      :fill-pointer num)))

DELETE-AT
CL> (delete-at 2 foo)
#(0 1 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 9)
CL> (setq foo (make-array 0 :fill-pointer t :adjustable t))
#()
CL> (dotimes (i 10) (vector-push-extend i foo))
NIL
CL> (delete-at 2 foo)
#(0 1 3 4 5 6 7 8 9)
CL> (delete-at 5 foo)
#(0 1 3 4 5 7 8 9)


Slobodan
0
Reply Slobodan 3/6/2010 9:25:34 PM

Slobodan Blazeski <slobodan.blazeski@gmail.com> writes:

> Ok will use below for now.
>
>  (defun delete-at (i seq)
>       (let ((slide (subseq seq (1+ i)))
> 	    (num (1- (fill-pointer seq))))
>         (replace seq slide  :start1 i)
> 	(adjust-array seq num
> 		      :fill-pointer num)))

REPLACE has sane behavior when both the source and target are the same
vector, and the ranges overlap, e.g.

  (replace vector vector :start1 i :start2 (1+ n))

Zach
0
Reply Zach 3/6/2010 9:33:49 PM

On Mar 6, 10:33=A0pm, Zach Beane <x...@xach.com> wrote:
> Slobodan Blazeski <slobodan.blaze...@gmail.com> writes:
> > Ok will use below for now.
>
> > =A0(defun delete-at (i seq)
> > =A0 =A0 =A0 (let ((slide (subseq seq (1+ i)))
> > =A0 =A0 =A0 =A0(num (1- (fill-pointer seq))))
> > =A0 =A0 =A0 =A0 (replace seq slide =A0:start1 i)
> > =A0 =A0(adjust-array seq num
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0:fill-pointer num)))
>
> REPLACE has sane behavior when both the source and target are the same
> vector, and the ranges overlap, e.g.
>
> =A0 (replace vector vector :start1 i :start2 (1+ n))
>
> Zach

If sequence-1 and sequence-2 are the same object and the region being
modified overlaps the region being copied from, then it is as if the
entire source region were copied to another place and only then copied
back into the target region. http://www.lispworks.com/documentation/lw50/CL=
HS/Body/f_replac.htm

Nice thanks

Slobodan

0
Reply Slobodan 3/6/2010 9:41:07 PM

Slobodan Blazeski <slobodan.blazeski@gmail.com> writes:

> I have an adjustable array :
>
>> (setq foo  (make-array 0  :element-type 'fixnum
>        :fill-pointer 0 :adjustable t))
> ; added some elements
>> foo
> #(1 2 1 3 4 1 1 5 101)
>
>
> How to remove say third element?
>

See http://groups.google.com/group/comp.lang.lisp/msg/8b3d664e9db85dde
where a sane definition of DELETE-NTH is given.  There's no need to
GENSYM or SUBSEQ anything.

Ariel
0
Reply Ariel 3/6/2010 10:14:03 PM

Slobodan Blazeski <slobodan.blazeski@gmail.com> writes:

> I have an adjustable array :
>
>> (setq foo  (make-array 0  :element-type 'fixnum
>        :fill-pointer 0 :adjustable t))
> ; added some elements
>> foo
> #(1 2 1 3 4 1 1 5 101)
>
>
> How to remove say third element?
>

See http://groups.google.com/group/comp.lang.lisp/msg/8b3d664e9db85dde
where a sane definition of DELETE-NTH is given.  There's no need to
GENSYM or SUBSEQ anything.

Ariel
0
Reply Ariel 3/6/2010 10:15:36 PM

On Mar 6, 10:24=A0pm, Slobodan Blazeski <slobodan.blaze...@gmail.com>
wrote:
> I have an adjustable array :
>
> > (setq foo =A0(make-array 0 =A0:element-type 'fixnum
>
> =A0 =A0 =A0 =A0:fill-pointer 0 :adjustable t))
> ; added some elements> foo
>
> #(1 2 1 3 4 1 1 5 101)
>
> How to remove say third element?
>

See http://groups.google.com/group/comp.lang.lisp/msg/8b3d664e9db85dde
where a sane definition of DELETE-NTH is given.  There's no need to
GENSYM or SUBSEQ anything.

Ariel
0
Reply Ariel 3/6/2010 10:17:00 PM

On Mar 6, 11:17=A0pm, Ariel Badichi <vermilionr...@gmail.com> wrote:
> On Mar 6, 10:24=A0pm, Slobodan Blazeski <slobodan.blaze...@gmail.com>
> wrote:
>
> > I have an adjustable array :
>
> > > (setq foo =A0(make-array 0 =A0:element-type 'fixnum
>
> > =A0 =A0 =A0 =A0:fill-pointer 0 :adjustable t))
> > ; added some elements> foo
>
> > #(1 2 1 3 4 1 1 5 101)
>
> > How to remove say third element?
>
> Seehttp://groups.google.com/group/comp.lang.lisp/msg/8b3d664e9db85dde
> where a sane definition of DELETE-NTH is given. =A0There's no need to
> GENSYM or SUBSEQ anything.
>
> Ariel

Looks good thanks.

Slobodan
0
Reply Slobodan 3/6/2010 10:46:42 PM

8 Replies
198 Views

(page loaded in 0.14 seconds)

Similiar Articles:













7/26/2012 5:03:34 PM


Reply: