2 [ substitute_eq
/4, % +OldVal
, +OldList
, +NewVal
, -NewList
3 memberchk_eq
/2, % +Val
, +List
4 intersect_eq
/3, % +List1
, +List2
, -Intersection
5 list_difference_eq
/3, % +List
, -Subtract
, -Rest
6 take
/3, % +N
, +List
, -FirstElements
7 drop
/3, % +N
, +List
, -LastElements
8 split_at
/4, % +N
, +List
, -FirstElements
, -LastElements
9 max_go_list
/2, % +List
, -Max
10 or_list
/2, % +ListOfInts
, -BitwiseOr
11 sublist
/2, % ?Sublist
, +List
12 bounded_sublist
/3, % ?Sublist
, +List
, +Bound
17 make_get_store_goal
/3,
18 make_update_store_goal
/3,
19 make_init_store_goal
/3,
27 :- use_module
(library
(lists
)).
28 :- use_module
(library
(assoc
)).
30 empty_ds
(DS
) :- empty_assoc
(DS
).
31 ds_to_list
(DS
,LIST
) :- assoc_to_list
(DS
,LIST
).
32 get_ds
(A
,B
,C
) :- get_assoc
(A
,B
,C
).
33 put_ds
(A
,B
,C
,D
) :- put_assoc
(A
,B
,C
,D
).
36 init_store
(Name
,Value
) :- nb_setval
(Name
,Value
).
38 get_store
(Name
,Value
) :- nb_getval
(Name
,Value
).
40 update_store
(Name
,Value
) :- b_setval
(Name
,Value
).
42 make_init_store_goal
(Name
,Value
,Goal
) :- Goal
= nb_setval
(Name
,Value
).
44 make_get_store_goal
(Name
,Value
,Goal
) :- Goal
= nb_getval
(Name
,Value
).
46 make_update_store_goal
(Name
,Value
,Goal
) :- Goal
= b_setval
(Name
,Value
).
49 /*******************************
50 * MORE LIST OPERATIONS
*
51 *******************************/
53 % substitute_eq
(+OldVal
, +OldList
, +NewVal
, -NewList
)
55 % Substitute OldVal by NewVal
in OldList
and unify the result
58 substitute_eq
(_
, [], _
, []) :- ! .
59 substitute_eq
(X
, [U
|Us
], Y
, [V
|Vs
]) :-
62 substitute_eq
(X
, Us
, Y
, Vs
)
64 substitute_eq
(X
, Us
, Y
, Vs
)
67 % memberchk_eq
(+Val
, +List
)
69 % Deterministic check of membership using
== rather than
72 memberchk_eq
(X
, [Y
|Ys
]) :-
78 % :- load_foreign_library
(chr_support
).
80 % list_difference_eq
(+List
, -Subtract
, -Rest
)
82 % Delete all elements of Subtract from List
and unify the result
83 % with Rest
. Element comparision is done using
==/2.
85 list_difference_eq
([],_
,[]).
86 list_difference_eq
([X
|Xs
],Ys
,L
) :-
88 -> list_difference_eq
(Xs
,Ys
,L
)
90 list_difference_eq
(Xs
,Ys
,T
)
93 % intersect_eq
(+List1
, +List2
, -Intersection
)
95 % Determine the intersection of two lists without unifying
values.
97 intersect_eq
([], _
, []).
98 intersect_eq
([X
|Xs
], Ys
, L
) :-
101 intersect_eq
(Xs
, Ys
, T
)
102 ; intersect_eq
(Xs
, Ys
, L
)
106 % take
(+N
, +List
, -FirstElements
)
108 % Take the first N elements from List
and unify this with
109 % FirstElements
. The definition is based on the GNU
-Prolog lists
110 % library
. Implementation by Jan Wielemaker
.
113 take
(N
, [H
|TA
], [H
|TB
]) :-
118 % Drop the first N elements from List
and unify the remainder with
121 drop
(0,LastElements
,LastElements
) :- !.
122 drop
(N
,[_
|Tail
],LastElements
) :-
125 drop
(N1
,Tail
,LastElements
).
127 split_at
(0,L
,[],L
) :- !.
128 split_at
(N
,[H
|T
],[H
|L1
],L2
) :-
132 % max_go_list
(+List
, -Max
)
134 % Return the maximum of List
in the standard order of terms
.
136 max_go_list
([H
|T
], Max
) :-
137 max_go_list
(T
, H
, Max
).
139 max_go_list
([], Max
, Max
).
140 max_go_list
([H
|T
], X
, Max
) :-
142 -> max_go_list
(T
, X
, Max
)
143 ; max_go_list
(T
, H
, Max
)
146 % or_list
(+ListOfInts
, -BitwiseOr
)
148 % Do a bitwise disjuction over all integer members of ListOfInts
.
154 or_list
([H
|T
], Or0
, Or
) :-
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 sublist
(Sub
, [H
|T
]) :-
162 '$sublist1'(T
, H
, Sub
).
164 '$sublist1'(Sub
, _
, Sub
).
165 '$sublist1'([H
|T
], _
, Sub
) :-
166 '$sublist1'(T
, H
, Sub
).
167 '$sublist1'([H
|T
], X
, [X
|Sub
]) :-
168 '$sublist1'(T
, H
, Sub
).
170 bounded_sublist
(Sublist
,_
,_
) :-
172 bounded_sublist
(Sublist
,[H
|List
],Bound
) :-
177 bounded_sublist
(Rest
,List
,NBound
)
179 bounded_sublist
(Sublist
,List
,Bound
)
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 chr_delete
([], _
, []).
186 chr_delete
([H
|T
], X
, L
) :-