From: ·········@gmail.com
Subject: T_T why isnt this doing what it should
Date: 
Message-ID: <1a088f0d-223b-41bf-90d8-eeffbb8bd8f1@26g2000hsk.googlegroups.com>
when i do something like

(setf a '("hey"))
(delete "hey" a :test #'string=)

it wont ever delete it....T_T i cant possibly understand why not...
plz help... i cant figure it out

From: Filipe Cabecinhas
Subject: Re: T_T why isnt this doing what it should
Date: 
Message-ID: <m2wsl0j270.fsf@farnsworth.albasani.net>
·········@gmail.com writes:

> when i do something like
>
> (setf a '("hey"))
> (delete "hey" a :test #'string=)
>
> it wont ever delete it....T_T i cant possibly understand why not...
> plz help... i cant figure it out

You have to read the documentation before asking here:
http://www.lisp.org/HyperSpec/Body/fun_removecm__elete-if-not.html

To make a long story short: use the return value, don't assume it will
modify the binding. #'DELETE may use side-effects, but it won't
guarantee what you want is done if you don't use its return value.

-- 
  - Filipe Cabecinhas

(defvar real-email
  (apply #'concatenate 'string
         '("filcab" ·@" "gmail" "." "com"))
  "My real email address.")
From: Kaz Kylheku
Subject: Re: T_T why isnt this doing what it should
Date: 
Message-ID: <8423c6c4-cf11-4838-8b38-9f01a9e5b8fe@a32g2000prf.googlegroups.com>
On Jun 7, 2:34 pm, ·········@gmail.com wrote:
> when i do something like
>
> (setf a '("hey"))

This is undefined behavior if the variable A is not defined first. You
did create the variable with a LET, DEFVAR, et cetera?

> (delete "hey" a :test #'string=)

What is stored in the variable A is a literal list. Such a list cannot
be destructively modified. You shouldn't be using DELETE on literal
lists.

Even if you fix that problem, you still have the issue that the Lisp
representation of lists is not purely referential. It is not possible
to destructively modify a non-empty list to make it an empty one and
vice versa.

A Lisp list is one of two things: the symbol NIL denoting an empty
list, or a pointer to the fist cons cell.

Given a pointer to a cons cell (non-empty list), there is nothing you
can do to the cons cell to make it look like an empty list. You cannot
keep that same pointer and have it refer to an empty list. That
pointer can only point to the cons cell and that's that. To get an
empty list, you must replace that pointer with the value NIL. I.e. you
must update the memory location which holds your list with the NIL
value.

A good idiom for this is:

  (setf a (delete "key" a :test #'string=))

Since DELETE returns the updated list, you take the return value and
stick it back into the original memory location. Sometimes this
assignment will in fact have no effect, but sometimes it will have a
necessary effect.
From: Daniel Weinreb
Subject: Re: T_T why isnt this doing what it should
Date: 
Message-ID: <CBP2k.1859$3j2.458@trnddc03>
·········@gmail.com wrote:
> when i do something like
> 
> (setf a '("hey"))
> (delete "hey" a :test #'string=)
> 
> it wont ever delete it....T_T i cant possibly understand why not...
> plz help... i cant figure it out

Don't feel bad.  This is a very common misunderstanding for new Lisp
users.

(setf a (delete "hey" a :test #'string=))

will do what you want.  Think about how a list is made up
of CONS cells.

Everybody: the fact that Lisp lists do not act like a
"collection object", in this sense, has always struck
me as a stumbling block for new users.  I wish there
were a way to present Lisp that helped new users avoid
this confusion.

If you type "lisp collections" into Google, the first
thing you get is a well-composed comp.lang.lisp post
from 2004 explaining this issue:

http://coding.derkeiler.com/Archive/Lisp/comp.lang.lisp/2004-09/1247.html


The first answer given was: use an adjustable one-D array
with a fill pointer.  Peter Seibel and Pascal Costanza
go on to talk about using sequence functions on such an array.

But then the original poster says "I don't see a way to
remove an object based on its index."  The original
answerer (Adam Warner) provides a seven-line function of his
own, untested.  Vassil Nikolov suggests

(setq v (delete-if #'(lambda (ignored) t) v :start i :count 1))

which is indeed shorter but still rather verbose and depends
on the setq since delete-if does not promise to be destructive.
It's really not a solution to the problem at all.

The chapter in Practical Common Lisp on "Collections" is about
vectors and hash tables.  I don't see an answer to his question
about a destructive removal based on element number.

You'd think someone must have written and made available a
handy Lisp collections utility, sort of a la what Java has,
but I did an extremely quick survey and didn't find one.
cl-containers seems to focus on somewhat more exotic data
structures.  Probably I'm just failing to come across it.
A kind of list object allowing for destructive deletion that
would work for the first element in a purely for-effect way
might be useful.
From: Ariel Badichi
Subject: Re: T_T why isnt this doing what it should
Date: 
Message-ID: <3b01e5da-06fd-4461-bc60-c6c3911b850b@d1g2000hsg.googlegroups.com>
On Jun 8, 2:43 pm, Daniel Weinreb <····@alum.mit.edu> wrote:
>
> But then the original poster says "I don't see a way to
> remove an object based on its index."  The original
> answerer (Adam Warner) provides a seven-line function of his
> own, untested.  Vassil Nikolov suggests
>
> (setq v (delete-if #'(lambda (ignored) t) v :start i :count 1))
>
> which is indeed shorter but still rather verbose and depends
> on the setq since delete-if does not promise to be destructive.
> It's really not a solution to the problem at all.

It looks like a solution to me, though I would write:

  (setq v (delete-nth v i))

Granted, you need to implement DELETE-NTH:

  (defun delete-nth (sequence n)
    (delete-if (constantly t) sequence :start n :count 1))

Defining a modify macro is also a possibility:

  (define-modify-macro delete-nth* (n) delete-nth)

Then you can just write:

  (delete-nth* v i)

> You'd think someone must have written and made available a
> handy Lisp collections utility, sort of a la what Java has,
> but I did an extremely quick survey and didn't find one.
> cl-containers seems to focus on somewhat more exotic data
> structures.  Probably I'm just failing to come across it.
> A kind of list object allowing for destructive deletion that
> would work for the first element in a purely for-effect way
> might be useful.

The library you mention (cl-containers) does have a list
container, and also a DELETE-ITEM-AT operator, though it
doesn't seem to work on list containers.  It's not usually
the case that you should use a list if you want to work
with indices.

Ariel
From: Daniel Weinreb
Subject: Re: T_T why isnt this doing what it should
Date: 
Message-ID: <NBP2k.1860$3j2.621@trnddc03>
·········@gmail.com wrote:
> when i do something like
> 
> (setf a '("hey"))
> (delete "hey" a :test #'string=)
> 
> it wont ever delete it....T_T i cant possibly understand why not...
> plz help... i cant figure it out

Don't feel bad.  This is a very common misunderstanding for new Lisp
users.

(setf a (delete "hey" a :test #'string=))

will do what you want.  Think about how a list is made up
of CONS cells.

Everybody: the fact that Lisp lists do not act like a
"collection object", in this sense, has always struck
me as a stumbling block for new users.  I wish there
were a way to present Lisp that helped new users avoid
this confusion.

If you type "lisp collections" into Google, the first
thing you get is a well-composed comp.lang.lisp post
from 2004 explaining this issue:

http://coding.derkeiler.com/Archive/Lisp/comp.lang.lisp/2004-09/1247.html


The first answer given was: use an adjustable one-D array
with a fill pointer.  Peter Seibel and Pascal Costanza
go on to talk about using sequence functions on such an array.

But then the original poster says "I don't see a way to
remove an object based on its index."  The original
answerer (Adam Warner) provides a seven-line function of his
own, untested.  Vassil Nikolov suggests

(setq v (delete-if #'(lambda (ignored) t) v :start i :count 1))

which is indeed shorter but still rather verbose and depends
on the setq since delete-if does not promise to be destructive.
It's really not a solution to the problem at all.

The chapter in Practical Common Lisp on "Collections" is about
vectors and hash tables.  I don't see an answer to his question
about a destructive removal based on element number.

You'd think someone must have written and made available a
handy Lisp collections utility, sort of a la what Java has,
but I did an extremely quick survey and didn't find one.
cl-containers seems to focus on somewhat more exotic data
structures.  Probably I'm just failing to come across it.
A kind of list object allowing for destructive deletion that
would work for the first element in a purely for-effect way
might be useful.
From: John Thingstad
Subject: Re: T_T why isnt this doing what it should
Date: 
Message-ID: <op.ucfk4uxeut4oq5@pandora.alfanett.no>
P� Sun, 08 Jun 2008 13:43:41 +0200, skrev Daniel Weinreb  
<···@alum.mit.edu>:

>
> Everybody: the fact that Lisp lists do not act like a
> "collection object", in this sense, has always struck
> me as a stumbling block for new users.  I wish there
> were a way to present Lisp that helped new users avoid
> this confusion.

Lisp functions are functional in that you can't modify arguments sent to  
the functions.
Since these arguments are often places (pointers) into the heap you can  
modify the heap content they point to.
delete is a destructive function. Unlike remove it is allowed (but not  
required) to make changes to the list which the at the location the  
argument points to. Since the original arguments are not modified you can  
only rely on the returned location to show the new location of the list.

Personally I find exceptions like push/pop more confusing.
(They work by macro side effect..)

Anyhow just present them with some functional examples that rely on the  
return value.
Then generalize this to list functions. Once you are in the 'Lisp frame of  
mind' it seems natural.

--------------
John Thingstad