Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for a pattern in a list

Status
Not open for further replies.

invalid6363

Programmer
Mar 21, 2008
24
US
i'm trying to write a predicate that takes a list and a pattern and determines if the pattern exists in the list.
for example:

list = [r,r,b,g,y]
pattern = r,r

occurs([X,Y|_],X,Y).
occurs([_|L],X,Y) :- occurs(L,X,Y).

this kind of works for a pattern of two, but if the pattern is r,b the result would be false.
any suggestions on how to fix this would be greatly appreciated.
thank you.
 
this seems to work for a pattern of two:

occurs([X,Y|_],X,Y).
occurs([X|Y],X,Y).
occurs([_|L],X,Y) :- occurs(L,X,Y).

 
what about for a pattern of three?

i tried just simply adding another variable :

occurs([X,Y,Z|_],X,Y,Z).
occurs([_|L],X,Y,Z) :- occurs(L,X,Y,Z).

but this does not work at all.

any suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top