1% ----------------------------------------------------------------------
2% BEGIN LICENSE BLOCK
3% Version: CMPL 1.1
4%
5% The contents of this file are subject to the Cisco-style Mozilla Public
6% License Version 1.1 (the "License"); you may not use this file except
7% in compliance with the License.  You may obtain a copy of the License
8% at www.eclipse-clp.org/license.
9%
10% Software distributed under the License is distributed on an "AS IS"
11% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
12% the License for the specific language governing rights and limitations
13% under the License.
14%
15% The Original Code is  The ECLiPSe Constraint Logic Programming System.
16% The Initial Developer of the Original Code is  Cisco Systems, Inc.
17% Portions created by the Initial Developer are
18% Copyright (C) 1995-2006 Cisco Systems, Inc.  All Rights Reserved.
19%
20% Contributor(s): IC-Parc, Imperal College London
21%
22% END LICENSE BLOCK
23%
24% System:	ECLiPSe Constraint Logic Programming System
25% Version:	$Id: varlists.pl,v 1.2 2009/07/16 09:11:24 jschimpf Exp $
26% ----------------------------------------------------------------------
27
28:- module(varlists).
29
30:- comment(categories, ["Data Structures"]).
31:- comment(summary, "Predicates to manipulate lists containing variables").
32:- comment(copyright, "Cisco Systems, Inc").
33:- comment(date, "$Date: 2009/07/16 09:11:24 $").
34
35
36:- export
37	delete/3,
38	intersection/3,
39	memberchk/2,
40	nonmember/2,
41	subtract/3,
42	union/3.
43
44
45memberchk(X,[Y|_]) :- X==Y, !.
46memberchk(X,[_|T]):- memberchk(X,T).
47
48
49nonmember(X,[Y|_]) :- X==Y, !,
50	fail.
51nonmember(Arg,[_|Tail]) :-
52	!,
53	nonmember(Arg,Tail).
54nonmember(_,[]).
55
56
57% delete (?Element, ?List, ?Result)
58% Result is List with Element removed
59delete(A, [B|C], C) :- A==B.
60delete(A, [B|C], [B|D]) :-
61	delete(A, C, D).
62
63% intersection(L1, L2, L3)
64% L3 is the intersection of L1 and L2, with arguments ordered as in L1
65
66intersection([], _, []).
67intersection([Head|L1tail], L2, L3) :-
68	memberchk(Head, L2),
69	!,
70	L3 = [Head|L3tail],
71	intersection(L1tail, L2, L3tail).
72intersection([_|L1tail], L2, L3) :-
73	intersection(L1tail, L2, L3).
74
75
76% subtract(L1, L2, L3)
77% L3 = L1 - L2
78
79subtract([], _, []).
80subtract([Head|L1tail], L2, L3) :-
81	memberchk(Head, L2),
82	!,
83	subtract(L1tail, L2, L3).
84subtract([Head|L1tail], L2, [Head|L3tail]) :-
85	subtract(L1tail, L2, L3tail).
86
87
88% union(L1, L2, L3)
89% L3 is (L1-L2) + L2
90
91union([], L, L).
92union([Head|L1tail], L2, L3) :-
93	memberchk(Head, L2),
94	!,
95	union(L1tail, L2, L3).
96union([Head|L1tail], L2, [Head|L3tail]) :-
97	union(L1tail, L2, L3tail).
98
99