[m-rev.] For review: additions to list.m
Ralph Becket
rafe at cs.mu.OZ.AU
Wed Jan 16 13:10:19 AEDT 2002
Estimated hours taken: 0.5
Branches: main
library/list.m:
Added predicates list__map_foldl/5 and list__map_foldr/5.
NEWS:
Recorded addition of new predicates.
Index: list.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/list.m,v
retrieving revision 1.103
diff -u -r1.103 list.m
--- list.m 29 Dec 2001 04:41:01 -0000 1.103
+++ list.m 16 Jan 2002 02:06:31 -0000
@@ -607,6 +607,30 @@
:- pred list__filter_map(pred(X, Y), list(X), list(Y), list(X)).
:- mode list__filter_map(pred(in, out) is semidet, in, out, out) is det.
+ % list__map_foldl(P, [X1, X2, .., Xn], [Y1, Y2, .., Yn], A0, A)
+ % is equivalent to
+ %
+ % P(X1, Y1, A0, A1),
+ % P(X2, Y2, A1, A2),
+ % ...
+ % P(Xn, Yn, An-1, A)
+ %
+:- pred list__map_foldl(pred(T1, T1, T2, T2), list(T1), list(T1), T2, T2).
+:- mode list__map_foldl(pred(in, out, in, out) is det, in, out, in, out) is det.
+:- mode list__map_foldl(pred(in, out, di, uo) is det, in, out, di, uo) is det.
+
+ % list__map_foldr(P, [X1, X2, .., Xn], [Y1, Y2, .., Yn], A0, A)
+ % is equivalent to
+ %
+ % P(Xn, Yn, A1, A0),
+ % ...
+ % P(X2, Y2, An-2, An-1),
+ % P(X1, Y1, An-1, A)
+ %
+:- pred list__map_foldr(pred(T1, T1, T2, T2), list(T1), list(T1), T2, T2).
+:- mode list__map_foldr(pred(in, out, in, out) is det, in, out, in, out) is det.
+:- mode list__map_foldr(pred(in, out, di, uo) is det, in, out, di, uo) is det.
+
% list__takewhile(Predicate, List, UptoList, AfterList) takes a
% closure with one input argument, and calls it on successive members
% of List as long as the calls succeed. The elements for which
@@ -1602,6 +1626,22 @@
Lo `..` Hi =
list__series(Lo, ( pred(I::in) is semidet :- I =< Hi ), plus(1)).
+
+%------------------------------------------------------------------------------%
+
+map_foldr(_, [], []) --> [].
+
+map_foldr(P, [X | Xs], [Y | Ys]) -->
+ map_foldr(P, Xs, Ys),
+ P(X, Y).
+
+%------------------------------------------------------------------------------%
+
+map_foldl(_, [], []) --> [].
+
+map_foldl(P, [X | Xs], [Y | Ys]) -->
+ P(X, Y),
+ map_foldl(P, Xs, Ys).
% ---------------------------------------------------------------------------- %
% ---------------------------------------------------------------------------- %
Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.237
diff -u -r1.237 NEWS
--- NEWS 15 Jan 2002 07:19:04 -0000 1.237
+++ NEWS 16 Jan 2002 02:09:09 -0000
@@ -65,6 +65,8 @@
Changes to the Mercury standard library:
+* We've added two predicates to list.m for mapping predicates with
+ accumulators: list__map_foldl/5 and list__map_foldr/5.
* We've added four functions to list.m for mapping functions over
corresponding members of lists: list__map_corresponding/3,
list__map_corresponding3/4, list__filter_map_corresponding/3
--------------------------------------------------------------------------
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