Permalink
Browse files

Add list.map_foldr/5.

library/list.m:
	As above.

NEWS:
	Announce the addition.
  • Loading branch information...
1 parent 833fdfb commit efad258248ddf6a20c5175942d4ba41ddceaf81a @juliensf juliensf committed Dec 27, 2013
Showing with 30 additions and 3 deletions.
  1. +1 −1 NEWS
  2. +29 −2 library/list.m
View
2 NEWS
@@ -19,7 +19,7 @@ Changes to the Mercury standard library:
det_min_key/1, det_max_key/1, foldl2_values/6 and foldl3_values/8.
* We have added the following predicates to the list module: foldr2/6,
- foldr3/8 and det_take/3.
+ foldr3/8, det_take/3 and map_foldr/5.
* We have added the following predicates to the bag module: foldl/4 and
foldl2/6.
View
@@ -1176,12 +1176,12 @@
% returned in End.
%
:- pred list.map_foldl(pred(L, M, A, A), list(L), list(M), A, A).
-:- mode list.map_foldl(pred(in, out, di, uo) is det, in, out, di, uo)
- is det.
:- mode list.map_foldl(pred(in, out, in, out) is det, in, out, in, out)
is det.
:- mode list.map_foldl(pred(in, out, mdi, muo) is det, in, out, mdi, muo)
is det.
+:- mode list.map_foldl(pred(in, out, di, uo) is det, in, out, di, uo)
+ is det.
:- mode list.map_foldl(pred(in, out, in, out) is semidet, in, out, in, out)
is semidet.
:- mode list.map_foldl(pred(in, out, mdi, muo) is semidet, in, out, mdi, muo)
@@ -1476,6 +1476,28 @@
:- mode list.map4_foldl(pred(in, out, out, out, out, di, uo) is cc_multi,
in, out, out, out, out, di, uo) is cc_multi.
+ % list.map_foldr(Pred, InList, OutList, Start, End) calls Pred
+ % with an accumulator (with the initial value of Start) on
+ % each element of InList (working right-to-left) to transform
+ % InList into OutList. The final value of the accumulator is
+ % returned in End.
+ %
+:- pred list.map_foldr(pred(L, M, A, A), list(L), list(M), A, A).
+:- mode list.map_foldr(pred(in, out, in, out) is det, in, out, in, out)
+ is det.
+:- mode list.map_foldr(pred(in, out, mdi, muo) is det, in, out, mdi, muo)
+ is det.
+:- mode list.map_foldr(pred(in, out, di, uo) is det, in, out, di, uo)
+ is det.
+:- mode list.map_foldr(pred(in, out, in, out) is semidet, in, out, in, out)
+ is semidet.
+:- mode list.map_foldr(pred(in, out, mdi, muo) is semidet, in, out, mdi, muo)
+ is semidet.
+:- mode list.map_foldr(pred(in, out, di, uo) is semidet, in, out, di, uo)
+ is semidet.
+:- mode list.map_foldr(pred(in, in, di, uo) is semidet, in, in, di, uo)
+ is semidet.
+
% list.filter_map_foldl(Transformer, List, TrueList, Start, End):
% Takes a predicate with one input argument, one output argument and an
% accumulator. It is called with each element of List. If a call succeeds,
@@ -2793,6 +2815,11 @@
P(H0, H1, H2, H3, H4, !A),
list.map4_foldl(P, T0, T1, T2, T3, T4, !A).
+list.map_foldr(_, [], [], !A).
+list.map_foldr(P, [H0 | T0], [H | T], !A) :-
+ list.map_foldr(P, T0, T, !A),
+ P(H0, H, !A).
+
list.filter_map_foldl(_, [], [], !A).
list.filter_map_foldl(P, [X | Xs], True, !A) :-
( P(X, Y, !A) ->

0 comments on commit efad258

Please sign in to comment.