mercredi 26 avril 2017

Prolog , check if a pair of elements appears in a list

Given a list , L , I would like to see if a pair , P , appears in the list . If the element is found , then print Found.

For example :

L = [3,1,2,5,4,7].
P = (2,5).

Given this example , I should get Found as 2,5 appears on 2nd and 3rd position in list L.

Here is my approach , but I am getting a false answer.

search_pair([_|[]],_).
search_pair([X|T],(F,L)) :-
   first(Y, T),
   write('searching: '),write(F),write(' '),write(L),nl,
   write('trying: '),write(X),write(' '),write(Y),nl,
   ((F == L , L = Y) -> (write('Found'))
   search_pair(T,(F,L),R).

Aucun commentaire:

Enregistrer un commentaire