* Added new files
[chr.git] / pairlist.pl
blobd7d15fe1e163e894da7dc09ea7cc8bc2b6f36604
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %% _ _ _ _
3 %% _ __ __ _(_)_ __| (_)___| |_
4 %% | '_ \ / _` | | '__| | / __| __|
5 %% | |_) | (_| | | | | | \__ \ |_
6 %% | .__/ \__,_|_|_| |_|_|___/\__|
7 %% |_|
8 %%
9 %% * author: Tom Schrijvers
11 :- module(pairlist,[
12 fst_of_pairs/2,
13 lookup/3,
14 lookup_any/3,
15 lookup_eq/3,
16 lookup_any_eq/3,
17 pairup/3,
18 snd_of_pairs/2,
19 translate/3
20 ]).
22 fst_of_pairs([],[]).
23 fst_of_pairs([X-_|XYs],[X|Xs]) :-
24 fst_of_pairs(XYs,Xs).
26 snd_of_pairs([],[]).
27 snd_of_pairs([_-Y|XYs],[Y|Ys]) :-
28 snd_of_pairs(XYs,Ys).
30 pairup([],[],[]).
31 pairup([X|Xs],[Y|Ys],[X-Y|XYs]) :-
32 pairup(Xs,Ys,XYs).
34 lookup([K - V | KVs],Key,Value) :-
35 ( K = Key ->
36 V = Value
38 lookup(KVs,Key,Value)
41 lookup_any([K - V | KVs],Key,Value) :-
43 K = Key,
44 V = Value
46 lookup_any(KVs,Key,Value)
49 lookup_eq([K - V | KVs],Key,Value) :-
50 ( K == Key ->
51 V = Value
53 lookup_eq(KVs,Key,Value)
56 lookup_any_eq([K - V | KVs],Key,Value) :-
58 K == Key,
59 V = Value
61 lookup_any_eq(KVs,Key,Value)
64 translate([],_,[]).
65 translate([X|Xs],Dict,[Y|Ys]) :-
66 lookup_eq(Dict,X,Y),
67 translate(Xs,Dict,Ys).
69 delete([], _, []).
70 delete([K - V| KVs], Key, PL) :-
71 ( Key = K ->
72 PL = KVs
74 PL = [ K - V | T ],
75 delete(KVs, Key, T)
78 delete_all([], _, []).
79 delete_all([K - V| KVs], Key, PL) :-
80 ( Key = K ->
81 delete_all(KVs, Key, PL)
84 PL = [ K - V | T ],
85 delete_all(KVs, Key, T)
88 delete_eq([], _, []).
89 delete_eq([K - V| KVs], Key, PL) :-
90 ( Key == K ->
91 PL = KVs
93 PL = [ K - V | T ],
94 delete_eq(KVs, Key, T)
97 delete_all_eq([], _, []).
98 delete_all_eq([K - V| KVs], Key, PL) :-
99 ( Key == K ->
100 delete_all_eq(KVs, Key, PL)
102 PL = [ K - V | T ],
103 delete_all_eq(KVs, Key, T)