Subject: Re: [SWIPL] Prolog

From: "Richard A. O'Keefe" <[email-masked]>
Date: Tue, 19 Dec 2000 11:46:19 +1300 (NZDT)

Nuno Baptista <[email-masked]> wrote:

	I'm trying to find [an] algorithm to find if there are equal
	elements in [two] given lists.

This is rather vague.  It sounds as though you want to know whether
two lists have a non-empty intersection.

	Here is the code :
	
	lista([], [], L):- !.
	lista([H1|T1], L, R):-
		member(H1, L),
		lista([H1|T1], L, [H1|R]).
	lista([H1|T1], [H2|T2], L):-
		lista(T1, T2, L).
	
This is _almost_ "compute the intersection", but the third clause
is strange.  Could it be that you want

    %   intersection(+As:list(T), +Bs:list(T), ?ABs:list(T))
    %   is true when ABs is a subsequence of As containing all
    %   and only the elements of As which are also elements of Bs.

    intersection([], _, []).
    intersection([A|As], Bs, ABs) :-
	\+ member(A, Bs),
	!,
	intersection(As, Bs, ABs).
    intersection([A|As], Bs, [A|ABs]) :-
	intersection(As, Bs, ABs).