[m-dev.] list__map_foldl

Tyson Richard DOWD trd at cs.mu.oz.au
Thu Jun 12 14:39:04 AEST 1997


> How about adding this predicate to the library?
> It is used 11 times in muz so it must be useful :-)

That's funny, I thought the same thing about cut in my LPT project ;-)

I've added list__map_foldl to the library.

===================================================================

Estimated hours taken: 0.2

library/list.m:
	Add list__map_foldl/5, an amalgam of list__map and list__foldl.
	This predicate was provided by Philip Dart.
NEWS:
	Mention this addition. Add arity to mentions of list__foldl2 and
	list__foldl.

Index: NEWS
===================================================================
RCS file: /home/staff/zs/imp/mercury/NEWS,v
retrieving revision 1.51
diff -u -r1.51 NEWS
--- NEWS	1997/06/12 04:19:35	1.51
+++ NEWS	1997/06/12 04:32:41
@@ -184,8 +184,10 @@
     lists using a user-specified procedure to write the elements and separating
     the elements with a user-specified separator string.
 
-  - We've added a double accumulator version of list__foldl called
-    list__foldl2, which is a convenient generalisation for accumulators
-    that also do I/O.
+  - We've added a double accumulator version of list__foldl/4 called
+    list__foldl2/6, which is a convenient generalisation for accumulators
+    that also do I/O. Also, we've added have list__map_foldl/5, which is an
+    amalgam of list__map/3 and list__foldl/4.
+
 
 
Index: library/list.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/list.m,v
retrieving revision 1.71
diff -u -r1.71 list.m
--- list.m	1997/06/12 04:19:41	1.71
+++ list.m	1997/06/12 04:29:53
@@ -402,6 +402,17 @@
 :- mode list__foldl2(pred(in, di, uo, di, uo) is det,
 		in, di, uo, di, uo) is det.
 
+	% list__map_foldl(Pred, InList, OutList, Start, End) calls Pred
+	% with an accumulator (with the initial value of Start) on
+	% each element of InList (working left-to-right) to transform
+	% InList into OutList.  The final value of the acumulator is
+	% returned in End.
+:- pred list__map_foldl(pred(X, Y, Z, Z), list(X), list(Y), Z, Z).
+:- 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, in, out) is semidet, in, out, in, out)
+                                                                is semidet.
+
 %-----------------------------------------------------------------------------%
 
 	% list__sort_and_remove_dups(Compare, Unsorted, Sorted) is true iff 
@@ -892,6 +903,12 @@
 list__foldl2(P, [H|T], FirstAcc0, FirstAcc, SecAcc0, SecAcc) :-
 	call(P, H, FirstAcc0, FirstAcc1, SecAcc0, SecAcc1),
 	list__foldl2(P, T, FirstAcc1, FirstAcc, SecAcc1, SecAcc).
+
+list__map_foldl(_, [],  []) -->
+        [].
+list__map_foldl(P, [H0|T0], [H|T]) -->
+        P(H0, H),
+        list__map_foldl(P, T0, T).
 
 list__foldr(_, [], Acc, Acc).
 list__foldr(P, [H|T], Acc0, Acc) :-

-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't eveyone's cup of
     trd at cs.mu.oz.au        #  fur.
http://www.cs.mu.oz.au/~trd #



More information about the developers mailing list