From: echo109
Subject: remove atoms from a list
Date: 
Message-ID: <638H9.47009$ID2.3632942@twister.southeast.rr.com>
Hello everyone,


I am new to LISP programming, and I have  a question. Does anyone know how
to delete an atom from a heirarchial list.


(remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a)) does not
work.

Please help.

Thanx

From: Chris Gehlker
Subject: Re: remove atoms from a list
Date: 
Message-ID: <BA126D10.2402D%gehlker@fastq.com>
On 12/3/02 1:00 PM, in article
·······················@twister.southeast.rr.com, "echo109"
<·······@carolina.rr.com> wrote:

> Hello everyone,
> 
> 
> I am new to LISP programming, and I have  a question. Does anyone know how
> to delete an atom from a heirarchial list.
> 
> 
> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a)) does not
> work.

I'm new to but here goes:

(defun nested-remove (obj lst)
  (let ((thing (first lst)))
    (cond ((null thing) nil)
          ((eql obj thing) (nested-remove obj (rest lst)))
          ((listp thing) (cons (nested-remove obj thing)
                               (nested-remove obj (rest lst))))
          (t (cons thing (nested-remove obj (rest lst)))))))

That leaves nils if the target was the only item in a sub-list. It's easy to
fix that if it's not what you want.

HTH



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Oleg
Subject: Re: remove atoms from a list
Date: 
Message-ID: <asj7m0$e6d$1@newsmaster.cc.columbia.edu>
Chris Gehlker wrote:

> On 12/3/02 1:00 PM, in article
> ·······················@twister.southeast.rr.com, "echo109"
> <·······@carolina.rr.com> wrote:
> 
>> Hello everyone,
>> 
>> 
>> I am new to LISP programming, and I have  a question. Does anyone know
>> how to delete an atom from a heirarchial list.
>> 
>> 
>> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a)) does
>> not work.
> 
> I'm new to but here goes:
> 
> (defun nested-remove (obj lst)
>   (let ((thing (first lst)))
>     (cond ((null thing) nil)
>           ((eql obj thing) (nested-remove obj (rest lst)))
>           ((listp thing) (cons (nested-remove obj thing)
>                                (nested-remove obj (rest lst))))
>           (t (cons thing (nested-remove obj (rest lst)))))))
> 
> That leaves nils if the target was the only item in a sub-list. It's easy
> to fix that if it's not what you want.
> 
 
Hello Chris,

Here is an example showing that your NESTED-REMOVE behaves differently from 
REMOVE on flat lists:

* (nested-remove 'a '(nil b))

NIL

Cheers,
Oleg
From: Chris Gehlker
Subject: Re: remove atoms from a list
Date: 
Message-ID: <BA12A9CF.24042%gehlker@fastq.com>
On 12/3/02 2:28 PM, in article ············@newsmaster.cc.columbia.edu,
"Oleg" <············@myrealbox.com> wrote:

> Chris Gehlker wrote:
> 
>> On 12/3/02 1:00 PM, in article
>> ·······················@twister.southeast.rr.com, "echo109"
>> <·······@carolina.rr.com> wrote:
>> 
>>> Hello everyone,
>>> 
>>> 
>>> I am new to LISP programming, and I have  a question. Does anyone know
>>> how to delete an atom from a heirarchial list.
>>> 
>>> 
>>> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a)) does
>>> not work.
>> 
>> I'm new to but here goes:
>> 
>> (defun nested-remove (obj lst)
>>   (let ((thing (first lst)))
>>     (cond ((null thing) nil)
>>           ((eql obj thing) (nested-remove obj (rest lst)))
>>           ((listp thing) (cons (nested-remove obj thing)
>>                                (nested-remove obj (rest lst))))
>>           (t (cons thing (nested-remove obj (rest lst)))))))
>> 
>> That leaves nils if the target was the only item in a sub-list. It's easy
>> to fix that if it's not what you want.
>> 
> 
> Hello Chris,
> 
> Here is an example showing that your NESTED-REMOVE behaves differently from
> REMOVE on flat lists:
> 
> * (nested-remove 'a '(nil b))
> 

Good catch, Oleg. Try substituting "null lst" for "null thing."



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Coby Beck
Subject: Re: remove atoms from a list
Date: 
Message-ID: <asj9eg$2ktq$1@otis.netspace.net.au>
"Oleg" <············@myrealbox.com> wrote in message
·················@newsmaster.cc.columbia.edu...
> Chris Gehlker wrote:
>
> > On 12/3/02 1:00 PM, in article
> > ·······················@twister.southeast.rr.com, "echo109"
> > <·······@carolina.rr.com> wrote:
> >
> >> Hello everyone,
> >>
> >>
> >> I am new to LISP programming, and I have  a question. Does anyone know
> >> how to delete an atom from a heirarchial list.
> >>
> >>
> >> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a))
does
> >> not work.
> >
> > I'm new to but here goes:
> >
> > (defun nested-remove (obj lst)
> >   (let ((thing (first lst)))
> >     (cond ((null thing) nil)
> >           ((eql obj thing) (nested-remove obj (rest lst)))
> >           ((listp thing) (cons (nested-remove obj thing)
> >                                (nested-remove obj (rest lst))))
> >           (t (cons thing (nested-remove obj (rest lst)))))))
> >
> > That leaves nils if the target was the only item in a sub-list. It's
easy
> > to fix that if it's not what you want.
> >
>
> Hello Chris,
>
> Here is an example showing that your NESTED-REMOVE behaves differently
from
> REMOVE on flat lists:
>
> * (nested-remove 'a '(nil b))
>

And when Chris is finished fixing that, he probably wants to support all the
keyword args you can give to remove.  This is the lambda-list you want to
support:

(defun nested-remove (item tree &key from-end test test-not start end count
key)
   .... )

Have fun! :)

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Marco Antoniotti
Subject: Re: remove atoms from a list
Date: 
Message-ID: <y6c7keqlmzj.fsf@octagon.valis.nyu.edu>
"Coby Beck" <·····@mercury.bc.ca> writes:

> "Oleg" <············@myrealbox.com> wrote in message
> ·················@newsmaster.cc.columbia.edu...
> > Chris Gehlker wrote:
> >
> > > On 12/3/02 1:00 PM, in article
> > > ·······················@twister.southeast.rr.com, "echo109"
> > > <·······@carolina.rr.com> wrote:
> > >
> > >> Hello everyone,
> > >>
> > >>
> > >> I am new to LISP programming, and I have  a question. Does anyone know
> > >> how to delete an atom from a heirarchial list.
> > >>
> > >>
> > >> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a))
> does
> > >> not work.
> > >
> > > I'm new to but here goes:
> > >
> > > (defun nested-remove (obj lst)
> > >   (let ((thing (first lst)))
> > >     (cond ((null thing) nil)
> > >           ((eql obj thing) (nested-remove obj (rest lst)))
> > >           ((listp thing) (cons (nested-remove obj thing)
> > >                                (nested-remove obj (rest lst))))
> > >           (t (cons thing (nested-remove obj (rest lst)))))))
> > >
> > > That leaves nils if the target was the only item in a sub-list. It's
> easy
> > > to fix that if it's not what you want.
> > >
> >
> > Hello Chris,
> >
> > Here is an example showing that your NESTED-REMOVE behaves differently
> from
> > REMOVE on flat lists:
> >
> > * (nested-remove 'a '(nil b))
> >
> 
> And when Chris is finished fixing that, he probably wants to support all the
> keyword args you can give to remove.  This is the lambda-list you want to
> support:
> 
> (defun nested-remove (item tree &key from-end test test-not start end count
> key)
>    .... )

This is an interesting exercise.  However, the semantics of :FROM-END
:START and :END make sense only if you specify the order in which you
traverse the tree (yep, it is a tree).  This is usually in pre-order,
but, of course, somebody is going to pop up asking for post-order and
in-order :)

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Harald Hanche-Olsen
Subject: Re: remove atoms from a list
Date: 
Message-ID: <pco65uak66l.fsf@thoth.math.ntnu.no>
+ Marco Antoniotti <·······@cs.nyu.edu>:

| This is an interesting exercise.  However, the semantics of
| :FROM-END :START and :END make sense only if you specify the order
| in which you traverse the tree (yep, it is a tree).  This is usually
| in pre-order, but, of course, somebody is going to pop up asking for
| post-order and in-order :)

That was my first reaction too, but usually you're really only
interested in removing leaves from the tree (see the subject line!),
and then the difference sort of evaporates, does it not?

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Yes it works in practice - but does it work in theory?
From: Chris Gehlker
Subject: Re: remove atoms from a list
Date: 
Message-ID: <BA12ACFA.24043%gehlker@fastq.com>
On 12/3/02 3:01 PM, in article ·············@otis.netspace.net.au, "Coby
Beck" <·····@mercury.bc.ca> wrote:

> 
> "Oleg" <············@myrealbox.com> wrote in message
> ·················@newsmaster.cc.columbia.edu...
>> Chris Gehlker wrote:
>> 
>>> On 12/3/02 1:00 PM, in article
>>> ·······················@twister.southeast.rr.com, "echo109"
>>> <·······@carolina.rr.com> wrote:
>>> 
>>>> Hello everyone,
>>>> 
>>>> 
>>>> I am new to LISP programming, and I have  a question. Does anyone know
>>>> how to delete an atom from a heirarchial list.
>>>> 
>>>> 
>>>> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a))
> does
>>>> not work.
>>> 
>>> I'm new to but here goes:
>>> 
>>> (defun nested-remove (obj lst)
>>>   (let ((thing (first lst)))
>>>     (cond ((null thing) nil)
>>>           ((eql obj thing) (nested-remove obj (rest lst)))
>>>           ((listp thing) (cons (nested-remove obj thing)
>>>                                (nested-remove obj (rest lst))))
>>>           (t (cons thing (nested-remove obj (rest lst)))))))
>>> 
>>> That leaves nils if the target was the only item in a sub-list. It's
> easy
>>> to fix that if it's not what you want.
>>> 
>> 
>> Hello Chris,
>> 
>> Here is an example showing that your NESTED-REMOVE behaves differently
> from
>> REMOVE on flat lists:
>> 
>> * (nested-remove 'a '(nil b))
>> 
> 
> And when Chris is finished fixing that, he probably wants to support all the
> keyword args you can give to remove.  This is the lambda-list you want to
> support:
> 
> (defun nested-remove (item tree &key from-end test test-not start end count
> key)
>  .... )
> 
> Have fun! :)

I'll try *some* of those. I think it will be fun.



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: marcel haesok
Subject: Re: remove atoms from a list
Date: 
Message-ID: <pyUH9.248703$QZ.39076@sccrnsc02>
> >> (remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a))
does
> >> not work.
> >
> > I'm new to but here goes:
> >
> > (defun nested-remove (obj lst)
> >   (let ((thing (first lst)))
> >     (cond ((null thing) nil)
> >           ((eql obj thing) (nested-remove obj (rest lst)))
> >           ((listp thing) (cons (nested-remove obj thing)
> >                                (nested-remove obj (rest lst))))
> >           (t (cons thing (nested-remove obj (rest lst)))))))
> >
> > That leaves nils if the target was the only item in a sub-list. It's
easy
> > to fix that if it's not what you want.
> >
>
> Hello Chris,
>
> Here is an example showing that your NESTED-REMOVE behaves differently
from
> REMOVE on flat lists:
>
> * (nested-remove 'a '(nil b))
>
> NIL
>
> Cheers,
> Oleg
>



The above solution produces the following.

---------------->(nested-remove 'a '(a b ((a)) c (a) d))--------------->
(B (NIL) C NIL D)


Is there any way we could produce this? ------------------> (B (()) c () d)

In other words, just removing 'a' without filling 'nil' in its place?
From: Chris Gehlker
Subject: Re: remove atoms from a list
Date: 
Message-ID: <BA15679F.24233%gehlker@fastq.com>
On 12/5/02 8:10 PM, in article ·····················@sccrnsc02, "marcel
haesok" <·········@attbi.com> wrote:

> The above solution produces the following.
> 
> ---------------->(nested-remove 'a '(a b ((a)) c (a) d))--------------->
> (B (NIL) C NIL D)
> 
> 
> Is there any way we could produce this? ------------------> (B (()) c () d)
> 
> In other words, just removing 'a' without filling 'nil' in its place?
> 

I don't see any difference save representation. Note that if you type the
latter in at the top level after ', Lisp will return the former.



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Coby Beck
Subject: Re: remove atoms from a list
Date: 
Message-ID: <asp8vr$21e6$1@otis.netspace.net.au>
"marcel haesok" <·········@attbi.com> wrote in message
··························@sccrnsc02...
>
> The above solution produces the following.
>
> ---------------->(nested-remove 'a '(a b ((a)) c (a) d))--------------->
> (B (NIL) C NIL D)
>
>
> Is there any way we could produce this? ------------------> (B (()) c ()
d)
>
> In other words, just removing 'a' without filling 'nil' in its place?

They are identical:
CL-USER 126 > '(b (nil) c nil d)
(B (NIL) C NIL D)

CL-USER 127 > '(b (()) c () d)
(B (NIL) C NIL D)

CL-USER 128 > (list nil () '() 'nil)
(NIL NIL NIL NIL)

As for making print your way, I don't know if any combination of the printer
variables will do that, but I wouldn't worry about it anyway.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: JP Massar
Subject: Re: remove atoms from a list
Date: 
Message-ID: <3ded1f4a.50743404@netnews.attbi.com>
On Tue, 03 Dec 2002 20:00:02 GMT, "echo109" <·······@carolina.rr.com>
wrote:

>Hello everyone,
>
>
>I am new to LISP programming, and I have  a question. Does anyone know how
>to delete an atom from a heirarchial list.
>
>
>(remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a)) does not
>work.
>
 
There's no Lisp primitive (like REMOVE) that will do what you ask.
You have to write it yourself.

Think about using recursion, and look at the MAPCAR function.
From: Harald Hanche-Olsen
Subject: Re: remove atoms from a list
Date: 
Message-ID: <pco7keqpxj2.fsf@fiinbeck.math.ntnu.no>
+ ······@alum.mit.edu (JP Massar):

| On Tue, 03 Dec 2002 20:00:02 GMT, "echo109" <·······@carolina.rr.com>
| wrote:
| 
| >I am new to LISP programming, and I have  a question. Does anyone know how
| >to delete an atom from a heirarchial list.
| >
| >
| >(remove 'a '(a b a c a)) works but (remove 'a '((a b)(a ) (a c) a)) does not
| >work.
| >
|  
| There's no Lisp primitive (like REMOVE) that will do what you ask.
| You have to write it yourself.
| 
| Think about using recursion, and look at the MAPCAR function.

Problem with with mapcar is that it produces one item in the output
for every item in the input, which is sort of not what you want.
I think this is an excellent place for the loop macro. Consider

(defun my-remove (item list)
  (loop for thing in list
	unless (eq thing item)
	collect thing))

which does the required thing on flat lists, and is easy to extend to
hierachical lists using recursion. (Once that is working, it's not too
hard to add some more bells and whistles, like a :test keyword
argument and so forth).

Fair warning to the beginner: The loop macro is powerful and
dangerous. You are well adviced to learn its simple uses first, and
refrain from using its full power until you have understood it
fully. There are many pitfalls for the unwary.

If it's ok to mutate the given list, a non-recursive implementation
could well be more appropriate, just traversing the tree and fixing
CDRs to bypass unwanted items where needed. See Erik Naggum's post
near the top of the "count symbols in a list", and ignore the
subsequent flurry of messages in that thread.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Yes it works in practice - but does it work in theory?
From: Thomas F. Burdick
Subject: Re: remove atoms from a list
Date: 
Message-ID: <xcvptsibu07.fsf@apocalypse.OCF.Berkeley.EDU>
Harald Hanche-Olsen <······@math.ntnu.no> writes:

> + ······@alum.mit.edu (JP Massar):
>
> | Think about using recursion, and look at the MAPCAR function.
> 
> Problem with with mapcar is that it produces one item in the output
> for every item in the input, which is sort of not what you want.

When you want something MAPCAR-like, but you want to be able to
sometimes collect two items, sometimes none, what you want is MAPCAN.

  (defun evens-only (list)
    (mapcan #'(lambda (item)
                (if (evenp item)
                    (list item)
                    '()))
      list))

> I think this is an excellent place for the loop macro.

LOOP is good too, though.

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Harald Hanche-Olsen
Subject: Re: remove atoms from a list
Date: 
Message-ID: <pcou1hulncr.fsf@thoth.math.ntnu.no>
+ Harald Hanche-Olsen <······@math.ntnu.no>:

| | Think about using recursion, and look at the MAPCAR function.
| 
| Problem with with mapcar is that it produces one item in the output
| for every item in the input, which is sort of not what you want.
| I think this is an excellent place for the loop macro.

Thomas Burdick pointed out mapcan, but on my way home from work it
dawned on me that your suggestion is sound too: There is an obvious
solution combining mapcar, remove and recursion that I had overlooked.

There. That should keep the original poster going for a while,
figuring out which suggestion he can work with.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Yes it works in practice - but does it work in theory?