2 [ nth
/3, % ?Index
, ?List
, ?Element
3 substitute_eq
/4, % +OldVal
, +OldList
, +NewVal
, -NewList
4 memberchk_eq
/2, % +Val
, +List
5 intersect_eq
/3, % +List1
, +List2
, -Intersection
6 list_difference_eq
/3, % +List
, -Subtract
, -Rest
7 take
/3, % +N
, +List
, -FirstElements
8 drop
/3, % +N
, +List
, -LastElements
9 split_at
/4, % +N
, +List
, -FirstElements
, -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 % nth
(?Index
, ?List
, ?Element
)
59 nth
(Index
, List
, Element
) :-
60 nth1
(Index
, List
, Element
).
63 % substitute_eq
(+OldVal
, +OldList
, +NewVal
, -NewList
)
65 % Substitute OldVal by NewVal
in OldList
and unify the result
68 substitute_eq
(_
, [], _
, []) :- ! .
69 substitute_eq
(X
, [U
|Us
], Y
, [V
|Vs
]) :-
72 substitute_eq
(X
, Us
, Y
, Vs
)
74 substitute_eq
(X
, Us
, Y
, Vs
)
77 % memberchk_eq
(+Val
, +List
)
79 % Deterministic check of membership using
== rather than
82 memberchk_eq
(X
, [Y
|Ys
]) :-
89 % list_difference_eq
(+List
, -Subtract
, -Rest
)
91 % Delete all elements of Subtract from List
and unify the result
92 % with Rest
. Element comparision is done using
==/2.
94 list_difference_eq
([],_
,[]).
95 list_difference_eq
([X
|Xs
],Ys
,L
) :-
97 -> list_difference_eq
(Xs
,Ys
,L
)
99 list_difference_eq
(Xs
,Ys
,T
)
102 % intersect_eq
(+List1
, +List2
, -Intersection
)
104 % Determine the intersection of two lists without unifying
values.
106 intersect_eq
([], _
, []).
107 intersect_eq
([X
|Xs
], Ys
, L
) :-
108 ( memberchk_eq
(X
, Ys
)
110 intersect_eq
(Xs
, Ys
, T
)
111 ; intersect_eq
(Xs
, Ys
, L
)
115 % take
(+N
, +List
, -FirstElements
)
117 % Take the first N elements from List
and unify this with
118 % FirstElements
. The definition is based on the GNU
-Prolog lists
119 % library
. Implementation by Jan Wielemaker
.
122 take
(N
, [H
|TA
], [H
|TB
]) :-
127 % Drop the first N elements from List
and unify the remainder with
130 drop
(0,LastElements
,LastElements
) :- !.
131 drop
(N
,[_
|Tail
],LastElements
) :-
134 drop
(N1
,Tail
,LastElements
).
136 split_at
(0,L
,[],L
) :- !.
137 split_at
(N
,[H
|T
],[H
|L1
],L2
) :-
141 % max_go_list
(+List
, -Max
)
143 % Return the maximum of List
in the standard order of terms
.
145 max_go_list
([H
|T
], Max
) :-
146 max_go_list
(T
, H
, Max
).
148 max_go_list
([], Max
, Max
).
149 max_go_list
([H
|T
], X
, Max
) :-
151 -> max_go_list
(T
, X
, Max
)
152 ; max_go_list
(T
, H
, Max
)
155 % or_list
(+ListOfInts
, -BitwiseOr
)
157 % Do a bitwise disjuction over all integer members of ListOfInts
.
163 or_list
([H
|T
], Or0
, Or
) :-
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 sublist
(Sub
, [H
|T
]) :-
171 '$sublist1'(T
, H
, Sub
).
173 '$sublist1'(Sub
, _
, Sub
).
174 '$sublist1'([H
|T
], _
, Sub
) :-
175 '$sublist1'(T
, H
, Sub
).
176 '$sublist1'([H
|T
], X
, [X
|Sub
]) :-
177 '$sublist1'(T
, H
, Sub
).
179 bounded_sublist
(Sublist
,_
,_
) :-
181 bounded_sublist
(Sublist
,[H
|List
],Bound
) :-
186 bounded_sublist
(Rest
,List
,NBound
)
188 bounded_sublist
(Sublist
,List
,Bound
)
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 min_list
([H
|T
], Min
) :-
194 '$min_list1'(T
, H
, Min
).
196 '$min_list1'([], Min
, Min
).
197 '$min_list1'([H
|T
], X
, Min
) :-
199 '$min_list1'(T
, X
, Min
)
200 ; '$min_list1'(T
, H
, Min
)
203 chr_delete
([], _
, []).
204 chr_delete
([H
|T
], X
, L
) :-