Preparing 5.6.56
[chr.git] / pairlist.pl
blobc709e57b305b50fc9da03ebb82123b4eb6acbd36
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 pairlist_delete_eq/3
21 ]).
23 fst_of_pairs([],[]).
24 fst_of_pairs([X-_|XYs],[X|Xs]) :-
25 fst_of_pairs(XYs,Xs).
27 snd_of_pairs([],[]).
28 snd_of_pairs([_-Y|XYs],[Y|Ys]) :-
29 snd_of_pairs(XYs,Ys).
31 pairup([],[],[]).
32 pairup([X|Xs],[Y|Ys],[X-Y|XYs]) :-
33 pairup(Xs,Ys,XYs).
35 lookup([K - V | KVs],Key,Value) :-
36 ( K = Key ->
37 V = Value
39 lookup(KVs,Key,Value)
42 lookup_any([K - V | KVs],Key,Value) :-
44 K = Key,
45 V = Value
47 lookup_any(KVs,Key,Value)
50 lookup_eq([K - V | KVs],Key,Value) :-
51 ( K == Key ->
52 V = Value
54 lookup_eq(KVs,Key,Value)
57 lookup_any_eq([K - V | KVs],Key,Value) :-
59 K == Key,
60 V = Value
62 lookup_any_eq(KVs,Key,Value)
65 translate([],_,[]).
66 translate([X|Xs],Dict,[Y|Ys]) :-
67 lookup_eq(Dict,X,Y),
68 translate(Xs,Dict,Ys).
70 pairlist_delete([], _, []).
71 pairlist_delete([K - V| KVs], Key, PL) :-
72 ( Key = K ->
73 PL = KVs
75 PL = [ K - V | T ],
76 pairlist_delete(KVs, Key, T)
79 pairlist_delete_all([], _, []).
80 pairlist_delete_all([K - V| KVs], Key, PL) :-
81 ( Key = K ->
82 pairlist_delete_all(KVs, Key, PL)
85 PL = [ K - V | T ],
86 pairlist_delete_all(KVs, Key, T)
89 pairlist_delete_eq([], _, []).
90 pairlist_delete_eq([K - V| KVs], Key, PL) :-
91 ( Key == K ->
92 PL = KVs
94 PL = [ K - V | T ],
95 pairlist_delete_eq(KVs, Key, T)
98 pairlist_delete_all_eq([], _, []).
99 pairlist_delete_all_eq([K - V| KVs], Key, PL) :-
100 ( Key == K ->
101 pairlist_delete_all_eq(KVs, Key, PL)
103 PL = [ K - V | T ],
104 pairlist_delete_all_eq(KVs, Key, T)