From: Dirt
Subject: [Q]: Help creating a find function
Date: 
Message-ID: <8ga79s0cpd5h3cq1c912js90m1mf8oafat@4ax.com>
Hello,
   I am trying to write a function which takes two arguments. The goal
is to determine if the first arg is a member of the second. Such as 
  
( fi 'a '(a b c) ) would return T
( fi 'a '(((a)) b c) ) would return T

( fi '(a) '(a b c) ) would return NIL

The code I have so far is ( and I'm really trying to fix my
indentation here )

(defun fi (a b)
  ( cond
    ((endp b) nil)
    ((equal a (car b)) t)
    ((listp (cdr b)) (fi a (car b)))))

This function obviously has problems. For example calling

( fi '(a) '(a b c) )

would result in an error: *** - ENDP: A true list must not end with A

I have traced through this and I think I understand why this happens.
The second time through, the atom A gets passed in and endp doesn't
like that.  I have spent several hours experimenting with this, trying
to use different variations of cdr and cars. However, when I fix one
case, i break another. I generally need to learn how to navigate
though nested lists so I can examine each list atom or list inside. I
appreciate the help.

Dirt

From: Rainer Joswig
Subject: Re: [Q]: Help creating a find function
Date: 
Message-ID: <rainer.joswig-995783.04492430012000@news.is-europe.net>
In article <··································@4ax.com>, Dirt 
<····@inam3.com> wrote:

> Hello,
>    I am trying to write a function which takes two arguments. The goal
> is to determine if the first arg is a member of the second. Such as 
>   
> ( fi 'a '(a b c) ) would return T
> ( fi 'a '(((a)) b c) ) would return T
> 
> ( fi '(a) '(a b c) ) would return NIL
> 
> The code I have so far is ( and I'm really trying to fix my
> indentation here )
> 
> (defun fi (a b)
>   ( cond
>     ((endp b) nil)
>     ((equal a (car b)) t)
>     ((listp (cdr b)) (fi a (car b)))))
> 
> This function obviously has problems.

Yep.

(defun my-member (item list)
  (cond
   ((null list) nil)
   ((equal item (first list)) t)
   (t (my-member item (rest list)))))

Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1, 
21079 Hamburg, Germany, Tel: +49 40 77175 226
Email: ·············@ision.de , WWW: http://www.ision.de/
From: Dirt
Subject: Re: [Q]: Help creating a find function
Date: 
Message-ID: <pft89sk68stnus28ls7qnnge80gf17dem3@4ax.com>
Hiya,

>(defun my-member (item list)
>  (cond
>   ((null list) nil)
>   ((equal item (first list)) t)
>   (t (my-member item (rest list)))))

this solution has similar problems to mine. by calling it such as:

( my-member 'a '((a)) ) 

it will fail. it comes thru the first time, the list is not null, so
it compares a to the first of the list which is (a). they are not
equal so it calls itself again with a and the rest of the list which
is nil at this point. the result is then nil, while it should be true.
thanks for the help though. ill keep at it.

Dirt 

On Sun, 30 Jan 2000 04:49:24 +0100, Rainer Joswig
<·············@ision.de> wrote:

>In article <··································@4ax.com>, Dirt 
><····@inam3.com> wrote:
>
>> Hello,
>>    I am trying to write a function which takes two arguments. The goal
>> is to determine if the first arg is a member of the second. Such as 
>>   
>> ( fi 'a '(a b c) ) would return T
>> ( fi 'a '(((a)) b c) ) would return T
>> 
>> ( fi '(a) '(a b c) ) would return NIL
>> 
>> The code I have so far is ( and I'm really trying to fix my
>> indentation here )
>> 
>> (defun fi (a b)
>>   ( cond
>>     ((endp b) nil)
>>     ((equal a (car b)) t)
>>     ((listp (cdr b)) (fi a (car b)))))
>> 
>> This function obviously has problems.
>
>Yep.
>
>(defun my-member (item list)
>  (cond
>   ((null list) nil)
>   ((equal item (first list)) t)
>   (t (my-member item (rest list)))))
>
>Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1, 
>21079 Hamburg, Germany, Tel: +49 40 77175 226
>Email: ·············@ision.de , WWW: http://www.ision.de/
From: Tunc Simsek
Subject: Re: [Q]: Help creating a find function
Date: 
Message-ID: <Pine.GSO.3.96.1000130094237.18342B-100000@paleale.EECS.Berkeley.EDU>
(defun my-member (item list)
  (cond
   ((null list) nil)
   ((equal item (first list)) t)
   ((listp (first list)) (or (my-member item (first list))
                     (my-member item (rest list))))
   (t (my-member item (rest list)))))

Tunc

On Sun, 30 Jan 2000, Dirt wrote:

> Hiya,
> 
> >(defun my-member (item list)
> >  (cond
> >   ((null list) nil)
> >   ((equal item (first list)) t)
> >   (t (my-member item (rest list)))))
> 
> this solution has similar problems to mine. by calling it such as:
> 
> ( my-member 'a '((a)) ) 
> 
> it will fail. it comes thru the first time, the list is not null, so
> it compares a to the first of the list which is (a). they are not
> equal so it calls itself again with a and the rest of the list which
> is nil at this point. the result is then nil, while it should be true.
> thanks for the help though. ill keep at it.
> 
> Dirt 
> 
> On Sun, 30 Jan 2000 04:49:24 +0100, Rainer Joswig
> <·············@ision.de> wrote:
> 
> >In article <··································@4ax.com>, Dirt 
> ><····@inam3.com> wrote:
> >
> >> Hello,
> >>    I am trying to write a function which takes two arguments. The goal
> >> is to determine if the first arg is a member of the second. Such as 
> >>   
> >> ( fi 'a '(a b c) ) would return T
> >> ( fi 'a '(((a)) b c) ) would return T
> >> 
> >> ( fi '(a) '(a b c) ) would return NIL
> >> 
> >> The code I have so far is ( and I'm really trying to fix my
> >> indentation here )
> >> 
> >> (defun fi (a b)
> >>   ( cond
> >>     ((endp b) nil)
> >>     ((equal a (car b)) t)
> >>     ((listp (cdr b)) (fi a (car b)))))
> >> 
> >> This function obviously has problems.
> >
> >Yep.
> >
> >(defun my-member (item list)
> >  (cond
> >   ((null list) nil)
> >   ((equal item (first list)) t)
> >   (t (my-member item (rest list)))))
> >
> >Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1, 
> >21079 Hamburg, Germany, Tel: +49 40 77175 226
> >Email: ·············@ision.de , WWW: http://www.ision.de/
> 
> 
> 
From: Rainer Joswig
Subject: Re: [Q]: Help creating a find function
Date: 
Message-ID: <rainer.joswig-5ED7BA.11411531012000@news.is-europe.net>
In article <··································@4ax.com>, Dirt 
<····@inam3.com> wrote:

> Hiya,
> 
> >(defun my-member (item list)
> >  (cond
> >   ((null list) nil)
> >   ((equal item (first list)) t)
> >   (t (my-member item (rest list)))))
> 
> this solution has similar problems to mine. by calling it such as:
> 
> ( my-member 'a '((a)) ) 

So you want a deep find. You just need to add a clause for that.

Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1, 
21079 Hamburg, Germany, Tel: +49 40 77175 226
Email: ·············@ision.de , WWW: http://www.ision.de/
From: Wolfgang Hukriede
Subject: Re: [Q]: Help creating a find function
Date: 
Message-ID: <8740ik$4b2$1@bossix.informatik.uni-kiel.de>
thats :

  (defun fi (x l)
      (cond ((equal x l) t)
            ((atom l) nil)
            (else (or (fi x (car l)) (fi x (cdr l))))))

hth, wh.