2 [ append
/2, % +ListOfLists
, -List
3 nth
/3, % ?Index
, ?List
, ?Element
4 substitute
/4, % +OldVal
, +OldList
, +NewVal
, -NewList
5 memberchk_eq
/2, % +Val
, +List
6 intersect_eq
/3, % +List1
, +List2
, -Intersection
7 list_difference_eq
/3, % +List
, -Subtract
, -Rest
8 take
/3, % +N
, +List
, -FirstElements
9 drop
/3, % +N
, +List
, -LastElements
10 max_go_list
/2, % +List
, -Max
11 or_list
/2, % +ListOfInts
, -BitwiseOr
12 sublist
/2, % ?Sublist
, +List
13 bounded_sublist
/3, % ?Sublist
, +List
, +Bound
19 make_get_store_goal
/3,
20 make_update_store_goal
/3,
21 make_init_store_goal
/3,
29 :- use_module
(library
(lists
)).
30 :- use_module
(library
(assoc
)).
32 empty_ds
(DS
) :- empty_assoc
(DS
).
33 ds_to_list
(DS
,LIST
) :- assoc_to_list
(DS
,LIST
).
34 get_ds
(A
,B
,C
) :- get_assoc
(A
,B
,C
).
35 put_ds
(A
,B
,C
,D
) :- put_assoc
(A
,B
,C
,D
).
38 init_store
(Name
,Value
) :- nb_setval
(Name
,Value
).
40 get_store
(Name
,Value
) :- nb_getval
(Name
,Value
).
42 update_store
(Name
,Value
) :- b_setval
(Name
,Value
).
44 make_init_store_goal
(Name
,Value
,Goal
) :- Goal
= nb_setval
(Name
,Value
).
46 make_get_store_goal
(Name
,Value
,Goal
) :- Goal
= nb_getval
(Name
,Value
).
48 make_update_store_goal
(Name
,Value
,Goal
) :- Goal
= b_setval
(Name
,Value
).
51 /*******************************
52 * MORE LIST OPERATIONS
*
53 *******************************/
55 % append
(+ListOfLists
, -List
)
57 % Convert a one
-level nested list into a flat one
. E
.g
.
58 % append
([[a
,b
], [c
]], X
) --> X
= [a
,b
,c
]. See also
67 % nth
(?Index
, ?List
, ?Element
)
71 nth
(Index
, List
, Element
) :-
72 nth1
(Index
, List
, Element
).
75 % substitute
(+OldVal
, +OldList
, +NewVal
, -NewList
)
77 % Substitute OldVal by NewVal
in OldList
and unify the result
78 % with NewList
. JW
: Shouldn
't this be called substitute_eq/4?
80 substitute(_, [], _, []) :- ! .
81 substitute(X, [U|Us], Y, [V|Vs]) :-
84 substitute(X, Us, Y, Vs)
86 substitute(X, Us, Y, Vs)
89 % memberchk_eq(+Val, +List)
91 % Deterministic check of membership using == rather than
94 memberchk_eq(X, [Y|Ys]) :-
101 % list_difference_eq(+List, -Subtract, -Rest)
103 % Delete all elements of Subtract from List and unify the result
104 % with Rest. Element comparision is done using ==/2.
106 list_difference_eq([],_,[]).
107 list_difference_eq([X|Xs],Ys,L) :-
109 -> list_difference_eq(Xs,Ys,L)
111 list_difference_eq(Xs,Ys,T)
114 % intersect_eq(+List1, +List2, -Intersection)
116 % Determine the intersection of two lists without unifying values.
118 intersect_eq([], _, []).
119 intersect_eq([X|Xs], Ys, L) :-
120 ( memberchk_eq(X, Ys)
122 intersect_eq(Xs, Ys, T)
123 ; intersect_eq(Xs, Ys, L)
127 % take(+N, +List, -FirstElements)
129 % Take the first N elements from List and unify this with
130 % FirstElements. The definition is based on the GNU-Prolog lists
131 % library. Implementation by Jan Wielemaker.
134 take(N, [H|TA], [H|TB]) :-
139 % Drop the first N elements from List and unify the remainder with
142 drop(0,LastElements,LastElements) :- !.
143 drop(N,[_|Tail],LastElements) :-
146 drop(N1,Tail,LastElements).
148 % max_go_list(+List, -Max)
150 % Return the maximum of List in the standard order of terms.
152 max_go_list([H|T], Max) :-
153 max_go_list(T, H, Max).
155 max_go_list([], Max, Max).
156 max_go_list([H|T], X, Max) :-
158 -> max_go_list(T, X, Max)
159 ; max_go_list(T, H, Max)
162 % or_list(+ListOfInts, -BitwiseOr)
164 % Do a bitwise disjuction over all integer members of ListOfInts.
170 or_list([H|T], Or0, Or) :-
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 sublist(Sub, [H|T]) :-
178 '$sublist1'(T, H, Sub).
180 '$sublist1'(Sub, _, Sub).
181 '$sublist1'([H|T], _, Sub) :-
182 '$sublist1'(T, H, Sub).
183 '$sublist1'([H|T], X, [X|Sub]) :-
184 '$sublist1'(T, H, Sub).
186 bounded_sublist(Sublist,_,_) :-
188 bounded_sublist(Sublist,[H|List],Bound) :-
193 bounded_sublist(Rest,List,NBound)
195 bounded_sublist(Sublist,List,Bound)
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 min_list([H|T], Min) :-
201 '$min_list1'(T, H, Min).
203 '$min_list1'([], Min, Min).
204 '$min_list1'([H|T], X, Min) :-
206 '$min_list1'(T, X, Min)
207 ; '$min_list1'(T, H, Min)
210 chr_delete([], _, []).
211 chr_delete([H|T], X, L) :-