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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Search

Status
Not open for further replies.

trica

Programmer
Joined
Jun 5, 2002
Messages
1
Location
UY
Hi everybody, I'm having a little problem with list seraching. I have to be able to find words in a word-puzzle.
I can't seem to solve the rule for searching a list of letters in another list.
Ex:
search([a,b,c],[[d,f,g],[e,a,b,c],N).
N=2.
I need to be able to find in what list it is, and I can't figure out how to go on after finding the first letter. Can anyone help me, please!
Thanx a lot.
 
Hello.
I cannot understand correctly what you want to do, but I guess...

*** program ***

samehead([], _).
samehead([X1|Xs], [X1|Ys]) :- samehead(Xs, Ys).

exist(X, Y) :- samehead(X, Y), !.
exist(X, [_|Y]) :- exist(X, Y).

search(X, [Y|_], 1) :- exist(X, Y), !.
search(X, [_|Ys], N) :- search(X, Ys, N2), N is N2 + 1.

*** sample ***

?- search([a, b, c], [[d, f, g], [e, a, b, c]], N).
N = 2 ;
no

?- search([a, b, c], [[d, f, g], [e, a, b]], N).
no

?- search([a, b, c], [[a, b, c, d, e], [a, b]], N).
N = 1 ;
no

?- search([a, b, c], [[a], , [a, b, c], [c]], N).
N = 3 ;
no

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top