2 [ prolog_flag
/3, % +Flag
, -Old
, +New
3 append_lists
/2, % +ListOfLists
, -List
4 nth
/3, % ?Index
, ?List
, ?Element
5 substitute
/4, % +OldVal
, +OldList
, +NewVal
, -NewList
6 memberchk_eq
/2, % +Val
, +List
7 intersect_eq
/3, % +List1
, +List2
, -Intersection
8 list_difference_eq
/3, % +List
, -Subtract
, -Rest
9 take
/3, % +N
, +List
, -FirstElements
10 max_go_list
/2, % +List
, -Max
11 or_list
/2, % +ListOfInts
, -BitwiseOr
18 :- use_module
(library
(lists
)).
20 % prolog_flag
(+Flag
, -Old
, +New
)
22 % Combine ISO prolog flag reading
and writing
24 prolog_flag
(Flag
, Old
, New
) :-
25 current_prolog_flag
(Flag
, Old
),
28 ; set_prolog_flag
(Flag
, New
)
32 /*******************************
33 * MORE LIST OPERATIONS
*
34 *******************************/
36 % append_lists
(+ListOfLists
, -List
)
38 % Convert a one
-level nested list into a flat one
. E
.g
.
39 % append_lists
([[a
,b
], [c
]], X
) --> X
= [a
,b
,c
]. See also
43 append_lists
([X
|Xs
],L
) :-
48 % nth
(?Index
, ?List
, ?Element
)
52 nth
(Index
, List
, Element
) :-
53 nth1
(Index
, List
, Element
).
56 % substitute
(+OldVal
, +OldList
, +NewVal
, -NewList
)
58 % Substitute OldVal by NewVal
in OldList
and unify the result
59 % with NewList
. JW
: Shouldn
't this be called substitute_eq/4?
61 substitute(_, [], _, []) :- ! .
62 substitute(X, [U|Us], Y, [V|Vs]) :-
65 substitute(X, Us, Y, Vs)
67 substitute(X, Us, Y, Vs)
70 % memberchk_eq(+Val, +List)
72 % Deterministic check of membership using == rather than
75 memberchk_eq(X, [Y|Ys]) :-
82 % list_difference_eq(+List, -Subtract, -Rest)
84 % Delete all elements of Subtract from List and unify the result
85 % with Rest. Element comparision is done using ==/2.
87 list_difference_eq([],_,[]).
88 list_difference_eq([X|Xs],Ys,L) :-
90 -> list_difference_eq(Xs,Ys,L)
92 list_difference_eq(Xs,Ys,T)
95 % intersect_eq(+List1, +List2, -Intersection)
97 % Determine the intersection of two lists without unifying values.
99 intersect_eq([], _, []).
100 intersect_eq([X|Xs], Ys, L) :-
101 ( memberchk_eq(X, Ys)
103 intersect_eq(Xs, Ys, T)
104 ; intersect_eq(Xs, Ys, L)
108 % take(+N, +List, -FirstElements)
110 % Take the first N elements from List and unify this with
111 % FirstElements. The definition is based on the GNU-Prolog lists
112 % library. Implementation by Jan Wielemaker.
115 take(N, [H|TA], [H|TB]) :-
121 % max_go_list(+List, -Max)
123 % Return the maximum of List in the standard order of terms.
125 max_go_list([H|T], Max) :-
126 max_go_list(T, H, Max).
128 max_go_list([], Max, Max).
129 max_go_list([H|T], X, Max) :-
131 -> max_go_list(T, X, Max)
132 ; max_go_list(T, H, Max)
135 % or_list(+ListOfInts, -BitwiseOr)
137 % Do a bitwise disjuction over all integer members of ListOfInts.
143 or_list([H|T], Or0, Or) :-
149 sublist(Sub, [H|T]) :-
150 '$sublist1'(T, H, Sub).
152 '$sublist1'(Sub, _, Sub).
153 '$sublist1'([H|T], _, Sub) :-
154 '$sublist1'(T, H, Sub).
155 '$sublist1'([H|T], X, [X|Sub]) :-
156 '$sublist1'(T, H, Sub).
158 min_list([H|T], Min) :-
159 '$min_list1'(T, H, Min).
161 '$min_list1'([], Min, Min).
162 '$min_list1'([H|T], X, Min) :-
164 '$min_list1'(T, X, Min)
165 ; '$min_list1'(T, H, Min)
168 chr_delete([], _, []).
169 chr_delete([H|T], X, L) :-
176 strip_attributes([],[]).
177 strip_attributes([V|R],[V2|R2]) :-
180 remove_attrs(V,VAttrs,V2)
183 strip_attributes(R,R2).
185 remove_attrs(_V,[],[]).
186 remove_attrs(V,att(X,Y,OtherAttrs),[(X,Y)|R]) :-
188 remove_attrs(V,OtherAttrs,R).
190 restore_attributes([],[]).
191 restore_attributes([_V|R],[[]|R2]) :-
192 restore_attributes(R,R2).
193 restore_attributes([V|R],[[(X,Y)|RVAttr]|R2]) :-
195 restore_attributes([V|R],[RVAttr|R2]).