10
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 ]). 22
23fst_of_pairs([],[]).
24fst_of_pairs([X-_|XYs],[X|Xs]) :-
25 fst_of_pairs(XYs,Xs).
26
27snd_of_pairs([],[]).
28snd_of_pairs([_-Y|XYs],[Y|Ys]) :-
29 snd_of_pairs(XYs,Ys).
30
31pairup([],[],[]).
32pairup([X|Xs],[Y|Ys],[X-Y|XYs]) :-
33 pairup(Xs,Ys,XYs).
34
35lookup([K - V | KVs],Key,Value) :-
36 ( K = Key ->
37 V = Value
38 ;
39 lookup(KVs,Key,Value)
40 ).
41
42lookup_any([K - V | KVs],Key,Value) :-
43 (
44 K = Key,
45 V = Value
46 ;
47 lookup_any(KVs,Key,Value)
48 ).
49
50lookup_eq([K - V | KVs],Key,Value) :-
51 ( K == Key ->
52 V = Value
53 ;
54 lookup_eq(KVs,Key,Value)
55 ).
56
57lookup_any_eq([K - V | KVs],Key,Value) :-
58 (
59 K == Key,
60 V = Value
61 ;
62 lookup_any_eq(KVs,Key,Value)
63 ).
64
65translate([],_,[]).
66translate([X|Xs],Dict,[Y|Ys]) :-
67 lookup_eq(Dict,X,Y),
68 translate(Xs,Dict,Ys).
69
70pairlist_delete_eq([], _, []).
71pairlist_delete_eq([K - V| KVs], Key, PL) :-
72 ( Key == K ->
73 PL = KVs
74 ;
75 PL = [ K - V | T ],
76 pairlist_delete_eq(KVs, Key, T)
77 )