My answer to the similar question Intersection and union of 2 lists might be of interest to you.
Unlike other answers posted here and there, the implementation I suggest is logically pure and monotone, which makes it more versatile and robust with regard to generalization / specialization.
First, let's see if it works with the query you gave above:
?- As = [la_defense,etoile,chatelet,nation],
Bs = [auber,chatelet,hotel_de_ville,nation],
list_list_intersection(As,Bs,Xs).
As = [la_defense, etoile, chatelet, nation],
Bs = [auber, chatelet, hotel_de_ville, nation],
Xs = [chatelet, nation].
But what if we write the query in a different (but logically equivalent) way?
?- As = [_,_,_,_],
Bs = [_,_,_,_],
list_list_intersection(As,Bs,Xs),
As = [la_defense,etoile,chatelet,nation],
Bs = [auber,chatelet,hotel_de_ville,nation].
As = [la_defense, etoile, chatelet, nation],
Bs = [auber, chatelet, hotel_de_ville, nation],
Xs = [chatelet, nation].
With list_list_intersection/3
we get the same result.
Now, let us consider using the builtin intersection/3
, which was proposed in another answer. Is intersection/3
robust regarding generalization, too?
?- As = [_,_,_,_],
Bs = [_,_,_,_],
intersection(As,Bs,Xs),
As = [la_defense,etoile,chatelet,nation],
Bs = [auber,chatelet,hotel_de_ville,nation].
false.
No! intersection/3
fails, even though it succeeded in a logically equivalent query, which shows that the implementation of intersection/3
is not monotone.
Bottom line: intersection/3
is harder to use right than list_list_intersection/3
; it forces you to think about declarative and procedural aspects when using it.