Find element in a list

Hy, to search for an element in a list I did it this way:

getListaPersoneUdienza(ListaPersone) :-
   findall(Y,personaPartecipaudienza(X,Y),ListaPersone).
trovaElementoLista(X,[X|_]).
trovaElementoLista(X,[_ | ListaPersone]) :- 
  getListaPersoneUdienza(ListaPersone),trovaElementoLista(X,ListaPersone). 

esempio(X) :-
   getListaPersoneUdienza(ListaPersone),trovaElementoLista(X,ListaPersone).

where the list is:

ListaPersone = [_14722, 'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AttoreA', 'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AvvocatoA',  'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AvvocatoB',  'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#CancelliereA',  'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#Catello_Maresca',  'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#ConvenutoA',  'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#Ufficiale_GiudiziarioA',  'http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AttoreA'|...] ;

if I find :

?- esempio('http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AttoreA').

the result is right, but if I enter this:

?- esempio('http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AvvocatoB').

the result is false, but it should be true.

  • Why not use member/2? it is made for searching through lists.
  • Why do you call getListaPersoneUdienza(ListaPersone) again on the remainder of the list in trovaElementoLista(X,[X|_]).? This is like saying the remainder of the list must equal the whole list. Try w/o that.

I tried to do this:
getListaPersoneUdienza(ListaPersone):-findall(Y,personaPartecipaudienza(X,Y),ListaPersone).

member(X,[X|_]).
member(X,[_ | getListaPersoneUdienza(ListaPersone)]):-member(X,getListaPersoneUdienza(ListaPersone)). 

esempio(X):-member(X,[_ | getListaPersoneUdienza(ListaPersone)]).

for this example

?- esempio('http://www.semanticweb.org/luigi/ontologies/2020/2/untitled-ontology-11#AttoreA').
it always returns true

I think, its this you have in mind:

item(m1).
item(m2).
item(m3).
    
item_list(Xs) :- findall(X, item(X), Xs).

is_member(X) :-
         item_list(Xs),
         member(X, Xs).

although … if there is no need to actually grab the whole list, then this can be simplified to a simple predicate lookup:

is_member(X) :- item(X).

1 Like