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 split_at
/4, % +N
, +List
, -FirstElements
, -LastElements
11 max_go_list
/2, % +List
, -Max
12 or_list
/2, % +ListOfInts
, -BitwiseOr
13 sublist
/2, % ?Sublist
, +List
14 bounded_sublist
/3, % ?Sublist
, +List
, +Bound
20 make_get_store_goal
/3,
21 make_update_store_goal
/3,
22 make_init_store_goal
/3,
30 :- use_module
(library
(lists
)).
31 :- use_module
(library
(assoc
)).
33 empty_ds
(DS
) :- empty_assoc
(DS
).
34 ds_to_list
(DS
,LIST
) :- assoc_to_list
(DS
,LIST
).
35 get_ds
(A
,B
,C
) :- get_assoc
(A
,B
,C
).
36 put_ds
(A
,B
,C
,D
) :- put_assoc
(A
,B
,C
,D
).
39 init_store
(Name
,Value
) :- nb_setval
(Name
,Value
).
41 get_store
(Name
,Value
) :- nb_getval
(Name
,Value
).
43 update_store
(Name
,Value
) :- b_setval
(Name
,Value
).
45 make_init_store_goal
(Name
,Value
,Goal
) :- Goal
= nb_setval
(Name
,Value
).
47 make_get_store_goal
(Name
,Value
,Goal
) :- Goal
= nb_getval
(Name
,Value
).
49 make_update_store_goal
(Name
,Value
,Goal
) :- Goal
= b_setval
(Name
,Value
).
52 /*******************************
53 * MORE LIST OPERATIONS
*
54 *******************************/
56 % append
(+ListOfLists
, -List
)
58 % Convert a one
-level nested list into a flat one
. E
.g
.
59 % append
([[a
,b
], [c
]], X
) --> X
= [a
,b
,c
]. See also
68 % nth
(?Index
, ?List
, ?Element
)
72 nth
(Index
, List
, Element
) :-
73 nth1
(Index
, List
, Element
).
76 % substitute
(+OldVal
, +OldList
, +NewVal
, -NewList
)
78 % Substitute OldVal by NewVal
in OldList
and unify the result
79 % with NewList
. JW
: Shouldn
't this be called substitute_eq/4?
81 substitute(_, [], _, []) :- ! .
82 substitute(X, [U|Us], Y, [V|Vs]) :-
85 substitute(X, Us, Y, Vs)
87 substitute(X, Us, Y, Vs)
90 % memberchk_eq(+Val, +List)
92 % Deterministic check of membership using == rather than
95 memberchk_eq(X, [Y|Ys]) :-
102 % list_difference_eq(+List, -Subtract, -Rest)
104 % Delete all elements of Subtract from List and unify the result
105 % with Rest. Element comparision is done using ==/2.
107 list_difference_eq([],_,[]).
108 list_difference_eq([X|Xs],Ys,L) :-
110 -> list_difference_eq(Xs,Ys,L)
112 list_difference_eq(Xs,Ys,T)
115 % intersect_eq(+List1, +List2, -Intersection)
117 % Determine the intersection of two lists without unifying values.
119 intersect_eq([], _, []).
120 intersect_eq([X|Xs], Ys, L) :-
121 ( memberchk_eq(X, Ys)
123 intersect_eq(Xs, Ys, T)
124 ; intersect_eq(Xs, Ys, L)
128 % take(+N, +List, -FirstElements)
130 % Take the first N elements from List and unify this with
131 % FirstElements. The definition is based on the GNU-Prolog lists
132 % library. Implementation by Jan Wielemaker.
135 take(N, [H|TA], [H|TB]) :-
140 % Drop the first N elements from List and unify the remainder with
143 drop(0,LastElements,LastElements) :- !.
144 drop(N,[_|Tail],LastElements) :-
147 drop(N1,Tail,LastElements).
149 split_at(0,L,[],L) :- !.
150 split_at(N,[H|T],[H|L1],L2) :-
154 % max_go_list(+List, -Max)
156 % Return the maximum of List in the standard order of terms.
158 max_go_list([H|T], Max) :-
159 max_go_list(T, H, Max).
161 max_go_list([], Max, Max).
162 max_go_list([H|T], X, Max) :-
164 -> max_go_list(T, X, Max)
165 ; max_go_list(T, H, Max)
168 % or_list(+ListOfInts, -BitwiseOr)
170 % Do a bitwise disjuction over all integer members of ListOfInts.
176 or_list([H|T], Or0, Or) :-
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 sublist(Sub, [H|T]) :-
184 '$sublist1'(T, H, Sub).
186 '$sublist1'(Sub, _, Sub).
187 '$sublist1'([H|T], _, Sub) :-
188 '$sublist1'(T, H, Sub).
189 '$sublist1'([H|T], X, [X|Sub]) :-
190 '$sublist1'(T, H, Sub).
192 bounded_sublist(Sublist,_,_) :-
194 bounded_sublist(Sublist,[H|List],Bound) :-
199 bounded_sublist(Rest,List,NBound)
201 bounded_sublist(Sublist,List,Bound)
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 min_list([H|T], Min) :-
207 '$min_list1'(T, H, Min).
209 '$min_list1'([], Min, Min).
210 '$min_list1'([H|T], X, Min) :-
212 '$min_list1'(T, X, Min)
213 ; '$min_list1'(T, H, Min)
216 chr_delete([], _, []).
217 chr_delete([H|T], X, L) :-