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

why is remove-if changing the type of input sequence?

21 views
Skip to first unread message

Karol Skocik

unread,
Jul 15, 2005, 11:21:28 AM7/15/05
to
Hi,
this is fine :

CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
:adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
0)))
T

while this is not :

CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
:adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
10)))
NIL

seems to me, that when no update is done to the input sequence, it's
fine, but when remove-if updates the input vector which has
fill-pointer, the result vector does not contain fill-pointer for some
reason.

is this meant to be so - or is there any reason for it? how to do it
other way?

thank you,
karol skocik

Edi Weitz

unread,
Jul 15, 2005, 11:31:57 AM7/15/05
to

The CLHS entry for REMOVE-IF says:

"If sequence is a vector, the result might or might not be simple,
and might or might not be identical to sequence."

So, obviously you can't rely on the result to have a fill pointer.

Cheers,
Edi.

--

Lisp is not dead, it just smells funny.

Real email: (replace (subseq "spam...@agharta.de" 5) "edi")

Pascal Costanza

unread,
Jul 15, 2005, 11:41:26 AM7/15/05
to

remove-if doesn't do anything to its argument. In general, it returns a
new sequence with the changed contents. There is no guarantee whether
the new sequence will have the same features as the input sequence, so
this means that in general, you cannot rely on the fact that the result
has, for example, a fill-pointer.

What you see happen in the first case is this: remove-if is allowed to
return its input argument when the result would be the same (equal)
anyway. Since the fill-pointer in the first case is 0, remove-if doesn't
see the elements, so by definition cannot do anything interesting. So it
just returns its input argument, which of course retains the fact that
it has a fill-pointer.


I hope this helps.


Pascal

--
2nd European Lisp and Scheme Workshop
July 26 - Glasgow, Scotland - co-located with ECOOP 2005
http://lisp-ecoop05.bknr.net/

Karol Skocik

unread,
Jul 15, 2005, 11:54:13 AM7/15/05
to
ok, i understand now. so i need to duplicate the input sequence and put
is as input to delete-if, which will just alter the contents, not the
properties of the sequence.

however, i was looking to copy-seq, and it unfortunately also drops the
fill-pointer. is there any general way to make duplicates of
structures or i need to do it manually?

thank you,
karol skocik

Pascal Costanza

unread,
Jul 15, 2005, 12:13:38 PM7/15/05
to
Karol Skocik wrote:
> ok, i understand now. so i need to duplicate the input sequence and put
> is as input to delete-if, which will just alter the contents, not the
> properties of the sequence.

Nope, this doesn't necessarily work either. delete-if is not required,
just allowed to change its input argument. If you want a destructive
operation, you need to do it "manually" by iterating over the array and
copying elements from old locations to new locations. sebseq and replace
may be of help, though.

> however, i was looking to copy-seq, and it unfortunately also drops the
> fill-pointer. is there any general way to make duplicates of
> structures or i need to do it manually?

A way to do what you seem to want is this:

(make-array (array-dimensions old-array)
... some properties, like fill-pointer ...
:initial-contents (loop for element across old-array
when (some-predicate-p element)
collect element))

Karol Skocik

unread,
Jul 15, 2005, 12:14:46 PM7/15/05
to
i got it - copy-structure.
but why is it not in CLTL2 by Steele? :-(

bye

Edi Weitz

unread,
Jul 15, 2005, 12:19:40 PM7/15/05
to
On 15 Jul 2005 09:14:46 -0700, "Karol Skocik" <Karol....@gmail.com> wrote:

> i got it - copy-structure.

COPY-STRUCTURE copies structures, not sequences.

> but why is it not in CLTL2 by Steele? :-(

CLtL2 is not the final definition of the ANSI standard. Consult the
CLHS instead.

<http://www.lispworks.com/documentation/HyperSpec/index.html>

Pascal Costanza

unread,
Jul 15, 2005, 12:30:05 PM7/15/05
to
Karol Skocik wrote:
> i got it - copy-structure.
> but why is it not in CLTL2 by Steele? :-(

copy-structure doesn't copy arrays, at least not as specified by ANSI
Common Lisp.

CLtL2 describes the state of Common Lisp as of 1989. Some changes have
been made between then and the final release of ANSI Common Lisp in 1994/5.

Karol Skocik

unread,
Jul 15, 2005, 12:40:51 PM7/15/05
to
ok, thanks. i realized after i post it that arrays are built-in, not
done by defstruct.

have a nice day,
karol skocik

Pascal Bourguignon

unread,
Jul 15, 2005, 2:06:22 PM7/15/05
to
Pascal Costanza <p...@p-cos.net> writes:

> Karol Skocik wrote:
>> ok, i understand now. so i need to duplicate the input sequence and put
>> is as input to delete-if, which will just alter the contents, not the
>> properties of the sequence.
>
> Nope, this doesn't necessarily work either. delete-if is not required,
> just allowed to change its input argument. If you want a destructive
> operation, you need to do it "manually" by iterating over the array
> and copying elements from old locations to new locations. sebseq and
> replace may be of help, though.
>
>> however, i was looking to copy-seq, and it unfortunately also drops the
>> fill-pointer. is there any general way to make duplicates of
>> structures or i need to do it manually?
>
> A way to do what you seem to want is this:
>
> (make-array (array-dimensions old-array)
> ... some properties, like fill-pointer ...
> :initial-contents (loop for element across old-array
> when (some-predicate-p element)
> collect element))
>

(defun copy-array (array &key copy-fill-pointer copy-adjustable
copy-displacement)
(when copy-displacement
(multiple-value-bind (disto disoff) (array-displacement array)
(when disto
(return-from copy-array
(make-array (array-dimensions array)
:element-type (array-element-type array)
:displaced-to disto
:displaced-index-offset disoff
:adjustable (when copy-adjustable
(adjustable-array-p array))
:fill-pointer (when copy-fill-pointer
(fill-pointer array)))))))
(let ((copy (make-array (array-dimensions array)
:adjustable (when copy-adjustable
(adjustable-array-p array))
:fill-pointer (when copy-fill-pointer
(fill-pointer array))
:element-type (array-element-type array))))
(dotimes (i (array-total-size copy))
(setf (row-major-aref copy i) (row-major-aref array i)))
copy))


(copy-array my-array :copy-fill-pointer t :copy-adjustable t
:copy-displacement t)


--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
__Pascal Bourguignon__ http://www.informatimago.com/

0 new messages