[m-rev.] More additions to list.m

Ralph Becket rafe at cs.mu.OZ.AU
Fri Dec 14 12:32:58 AEDT 2001


Estimated hours taken: 0.5
Branches: main

Additions to list.m.

library/list.m:
	Added list__filter_map_corresponding/3 and
	list__filter_map_corresponding3/4.

NEWS:
	Recorded new additions.

Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.230
diff -u -r1.230 NEWS
--- NEWS	14 Dec 2001 01:05:45 -0000	1.230
+++ NEWS	14 Dec 2001 01:31:09 -0000
@@ -64,6 +64,8 @@
   extensions which are unlikely to be implemented.
 
 Changes to the Mercury standard library:
+* We've added two new list functions to list.m, namely
+  list__filter_map_corresponding/3 and list__filter_map_corresponding3/4.
 * We've added a func version of error/1, called func_error/1, to require.m.
 * We've added two functions to list.m for mapping functions over
   corresponding members of lists (list__map_corresponding/3 and

Index: list.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/list.m,v
retrieving revision 1.101
diff -u -r1.101 list.m
--- list.m	14 Dec 2001 01:06:01 -0000	1.101
+++ list.m	14 Dec 2001 01:29:29 -0000
@@ -456,6 +456,30 @@
 :- func list__map_corresponding3(func(A, B, C) = D, list(A), list(B), list(C)) =
 		list(D).
 
+	% list__filter_map_corresponding/3 is like list__map_corresponding/3
+	% except the function argument is semidet and the output list
+	% consists of only those applications of the function argument that
+	% succeeded.
+	%
+:- func list__filter_map_corresponding(func(A, B) = C,
+		list(A), list(B)
+	) = list(C).
+:- mode list__filter_map_corresponding(func(in, in) = out is semidet,
+		in, in
+	) = out is det.
+
+	% list__filter_map_corresponding3/4 is like list__map_corresponding3/4
+	% except the function argument is semidet and the output list
+	% consists of only those applications of the function argument that
+	% succeeded.
+	%
+:- func list__filter_map_corresponding3(func(A, B, C) = D,
+		list(A), list(B), list(C)
+	) = list(D).
+:- mode list__filter_map_corresponding3(func(in, in, in) = out is semidet,
+		in, in, in
+	) = out is det.
+
 	% list__foldl(Pred, List, Start, End) calls Pred with each
 	% element of List (working left-to-right) and an accumulator
 	% (with the initial value of Start), and returns the final
@@ -1204,9 +1228,44 @@
 	  else if As = [],        Bs = [],        Cs = []
 	  then    []
 
-	  else    func_error(
-	  		"list__map_corresponding3: mismatched list arguments"
+	  else    func_error("list__map_corresponding3: \
+mismatched list arguments")
+	).
+
+
+
+list__filter_map_corresponding(_, [],       []      ) = [].
+
+list__filter_map_corresponding(_, [],       [_|_]   ) = 
+	func_error("list__filter_map_corresponding/3: \
+mismatched list arguments").
+
+list__filter_map_corresponding(_, [_|_],    []      ) =
+	func_error("list__filter_map_corresponding/3: \
+mismatched list arguments").
+
+list__filter_map_corresponding(F, [A | As], [B | Bs]) = 
+	( if   F(A, B) = C
+	  then [C | list__filter_map_corresponding(F, As, Bs)]
+	  else list__filter_map_corresponding(F, As, Bs)
+	).
+
+
+
+list__filter_map_corresponding3(F, As, Bs, Cs) =
+
+	( if      As = [A | As0], Bs = [B | Bs0], Cs = [C | Cs0]
+	  then
+	  	  ( if   F(A, B, C) = D
+	            then [D | list__filter_map_corresponding3(F, As0, Bs0, Cs0)]
+		    else list__filter_map_corresponding3(F, As0, Bs0, Cs0)
 		  )
+
+	  else if As = [],        Bs = [],        Cs = []
+	  then    []
+
+	  else    func_error("list__filter_map_corresponding3: \
+mismatched list arguments")
 	).
 
 
--------------------------------------------------------------------------
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