Comparing 2 lists 
Author Message
 Comparing 2 lists

Hello,

does anyone know how to compare 2 lists in prolog to find out if one
list is part of the other ??

I want to be able to find out if a short list (max. 16 Elements)
equals with a part of a longer list (max. 64 Elements).
For example I want to know if [a,b,c,d,e,f,g,h] includes [b,c,d,e].

So I need something like the member/2 command comparing lists.

Does anyone know if there is a command doing the job in SWI-prolog or
how to write a function solving the problem ?

I also need the position of the first element of the equal part. If
anyone knows a solution for thar one, I'd be pleased to hear it, too.

I appreciate any hint.

Thanks,

Mathias



Sun, 02 May 2004 19:13:07 GMT  
 Comparing 2 lists


Quote:
> Hello,

> does anyone know how to compare 2 lists in prolog to find out if one
> list is part of the other ??

> I want to be able to find out if a short list (max. 16 Elements)
> equals with a part of a longer list (max. 64 Elements).
> For example I want to know if [a,b,c,d,e,f,g,h] includes [b,c,d,e].

> So I need something like the member/2 command comparing lists.

> Does anyone know if there is a command doing the job in SWI-prolog or
> how to write a function solving the problem ?

> I also need the position of the first element of the equal part. If
> anyone knows a solution for thar one, I'd be pleased to hear it, too.

> I appreciate any hint.

> Thanks,

> Mathias

The Edinburgh Public Domain Library contains a number of list processing
predicates including:

subseq0/2

get or check subsequences of a list

 subseq0(Sequence, SubSequence)

 +Sequence <list>
 ?SubSequence <list> or <variable>

 subseq0(Sequence, SubSequence)
 is true when SubSequence is a subsequence of Sequence, but may
 be Sequence itself.   Thus subseq0([a,b], [a,b]) is true as well
 as subseq0([a,b], [a]).

 Example

  ?- bagof(S, subseq0([a,b], S), B).
  S = _ ,
  B = [[a,b],[b],[],[a]]

________________________________________________________________________

subseq1/2

get or check proper subsequences of a list

 subseq1(Sequence, SubSequence)

 +Sequence <list>
 ?SubSequence <list> or <variable>

 subseq1(Sequence, SubSequence)
 is true when SubSequence is a proper subsequence of Sequence,
 that is it contains at least one element less.

 Example

  ?- bagof(S, subseq1([a,b], S), B).
  S = _ ,
  B = [[b],[],[a]]

The library is at http://www.ifs.org.uk/~popx/prolog/library.html

Richard



Wed, 05 May 2004 02:55:59 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to compare infinite lists?

2. How to compare two lists ?

3. comparing multiple list lengths

4. Comparing two lists for differences

5. How does CPCS rank compared to List'n Label?

6. comparing all values of a list to regex

7. comparing lists

8. NEWBIE QUESTIION: Comparing Lists in Python

9. Comparing Lists

10. newbie: comparing mutually recursive lists

11. compare function in sort function of lists

12. compare lists

 

 
Powered by phpBB® Forum Software