compare lists 
Author Message
 compare lists

Hello,

I don't know to much about list and Prolog, but I must write a predicate who
will compare two lists.
For example:
Does List_1 has the same number of elements and if these elements are the
same like the elements in List_2.

Please help me. Greats Thanks fo help.

lemon




Sun, 30 May 2004 20:30:53 GMT  
 compare lists

Quote:

> Hello,

> I don't know to much about list and Prolog, but I must write a predicate who
> will compare two lists.
> For example:
> Does List_1 has the same number of elements and if these elements are the
> same like the elements in List_2.

use simply '='

will_succeed() :- [1,2,3] = [1,2,3].
will_fail() :- [1,2,3] = [1,2,3,4].

Quote:

> Please help me. Greats Thanks fo help.

> lemon



--
          Yoann  Padioleau,  INSA de Rennes, France,
Opinions expressed here are only mine. Je n'cris qu' titre personnel.
**____   Get Free. Be Smart.  Simply use Linux and Free Software.   ____**


Sun, 30 May 2004 20:37:33 GMT  
 compare lists

Quote:
> use simply '='

> will_succeed() :- [1,2,3] = [1,2,3].
> will_fail() :- [1,2,3] = [1,2,3,4].

Thanks, but this is a good solution only if i want to know does  list _1 has
the same number of elements what  list_2 does.
I need predicate who compare two lists and give me information that the
lists
has the same elements and number of elements also be the same.

Pawel Menich

lemon



Sun, 30 May 2004 23:25:31 GMT  
 compare lists

Quote:

> > use simply '='

> > will_succeed() :- [1,2,3] = [1,2,3].
> > will_fail() :- [1,2,3] = [1,2,3,4].

> Thanks, but this is a good solution only if i want to know does  list _1 has
> the same number of elements what  list_2 does.

not exactly, = go in depth, it look at the element too, so
 [1,2,3] = [3,4,5] will fail under prolog.

Quote:
> I need predicate who compare two lists and give me information that the
> lists
> has the same elements and number of elements also be the same.

But does the order important ?
do you want that
your_predicate([1,2,3],[3,2,1]) succeed or fail ?
if you want, so you want to know if List1 is a permutation of List2.
there is many way to write this predicate.
One way is first to check that they have the same length (length(List1,N1),length(List2,N2), N1 = N2)
and then to check that all the element of List1 are in List2 by using a recursive predicate,
the function member(Element, List) can be useful.

Quote:

> Pawel Menich

> lemon


--
          Yoann  Padioleau,  INSA de Rennes, France,
Opinions expressed here are only mine. Je n'cris qu' titre personnel.
**____   Get Free. Be Smart.  Simply use Linux and Free Software.   ____**


Mon, 31 May 2004 00:37:12 GMT  
 compare lists


Quote:
> One way is first to check that they have the same length

(length(List1,N1),length(List2,N2), N1 = N2)
Quote:
> and then to check that all the element of List1 are in List2 by using a

recursive predicate,

Quote:
> the function member(Element, List) can be useful.

Thanks for help ! :-)  Information from you help me finished my two little
Prolog programs.

Best regards,

Pawel Menich

lemon




Mon, 31 May 2004 06:41:02 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. comparing lists

2. NEWBIE QUESTIION: Comparing Lists in Python

3. Comparing Lists

4. comparing lists

5. Comparing list element with 'sant

6. comparing lists

7. comparing lists and/or tuples 'by pointer'

8. How to compare infinite lists?

9. comparing all values of a list to regex

10. How to compare two lists ?

11. newbie: comparing mutually recursive lists

12. Comparing 2 lists

 

 
Powered by phpBB® Forum Software