[m-rev.] diff: list__filter cleanup

Zoltan Somogyi zs at cs.mu.OZ.AU
Tue Mar 22 11:48:05 AEDT 2005


library/list.m:
	Make the implementation of filter and filter_map more straightforward
	by deleting contortions that used to be required for tail recursion in
	Prolog.

Zoltan.

cvs diff: Diffing .
Index: list.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/list.m,v
retrieving revision 1.134
diff -u -b -r1.134 list.m
--- list.m	15 Mar 2005 02:51:23 -0000	1.134
+++ list.m	21 Mar 2005 07:01:33 -0000
@@ -1675,35 +1675,35 @@
 	list__filter(P, Xs, Ys, _).
 
 list__filter(_, [],  [], []).
-list__filter(P, [H | T], L, M) :-
+list__filter(P, [H | T], True, False) :-
+	list__filter(P, T, TrueTail, FalseTail),
 	( call(P, H) ->
-		L = [H | L1],
-		M = M1
+		True = [H | TrueTail],
+		False = FalseTail
 	;
-		L = L1,
-		M = [H | M1]
-	),
-	list__filter(P, T, L1, M1).
+		True = TrueTail,
+		False = [H | FalseTail]
+	).
 
 list__filter_map(_, [],  []).
-list__filter_map(P, [H0 | T0], L) :-
+list__filter_map(P, [H0 | T0], True) :-
+	list__filter_map(P, T0, TrueTail),
 	( call(P, H0, H) ->
-		L = [H | L1]
+		True = [H | TrueTail]
 	;
-		L = L1
-	),
-	list__filter_map(P, T0, L1).
+		True = TrueTail
+	).
 
 list__filter_map(_, [], [], []).
-list__filter_map(P, [H0 | T0], L, M) :-
+list__filter_map(P, [H0 | T0], True, False) :-
+	list__filter_map(P, T0, TrueTail, FalseTail),
 	( call(P, H0, H) ->
-		L = [H | L1],
-		M = M1
+		True = [H | TrueTail],
+		False = FalseTail
 	;
-		L = L1,
-		M = [H0 | M1]
-	),
-	list__filter_map(P, T0, L1, M1).
+		True = TrueTail,
+		False = [H0 | FalseTail]
+	).
 
 list__takewhile(_, [], [], []).
 list__takewhile(P, [X | Xs], Ins, Outs) :-
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list