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

Ralph Becket rbeck at microsoft.com
Thu Apr 5 03:11:11 AEST 2001


Found this lying around...

- Ralph



Estimated hours taken: 0.25

library/list.m:
        Added list__cons/2 function.
        Added list__det_map2/3 function.

NEWS:
        Mentioned above additions.


Index: list.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/list.m,v
retrieving revision 1.97
diff -u -r1.97 list.m
--- list.m      2001/01/17 04:35:19     1.97
+++ list.m      2001/04/04 16:54:09
@@ -56,6 +56,10 @@

 
%-----------------------------------------------------------------------
------%

+       % Utility function: cons(X, Xs) = [X | Xs].
+       %
+:- func list__cons(T, list(T)) = list(T).
+
        % Standard append predicate:
        % list__append(Start, End, List) is true iff
        % `List' is the result of concatenating `Start' and `End'.
@@ -429,6 +433,15 @@
 :- mode list__map2(pred(in, out, out) is nondet, in, out, out) is
nondet.
 :- mode list__map2(pred(in, in, in) is semidet, in, in, in) is semidet.

+       % list__det_map2(Fn, ListA, ListB) = List:
+       %       If   ListA = [A1, A2, ..., An]
+       %       and  ListB = [B1, B2, ..., Bn]
+       %       then List  = [Fn(A1, B1), Fn(A2, B2), ..., Fn(An, Bn)].
+       %       An exception is raised if ListA and ListB have different
+       %       lengths.
+       %
+:- func list__det_map2(func(T1, T2) = T, list(T1), list(T2)) = list(T).
+
        % list__map3(T, L, M1, M2, M3) uses the closure T
        % to transform the elements of L into the elements of M1, M2 and
M3.
 :- pred list__map3(pred(A, B, C, D), list(A), list(B), list(C),
list(D)).
@@ -643,8 +656,12 @@
 
%-----------------------------------------------------------------------
------%

 :- implementation.
+
+:- import_module bintree_set, require, std_util.
+
+%----------------------------------------------------------------------
-------%

-:- import_module bintree_set, require, std_util.
+list__cons(X, Xs) = [X | Xs].

 
%-----------------------------------------------------------------------
------%

@@ -1421,5 +1438,16 @@
 Lo `..` Hi =
        list__series(Lo, ( pred(I::in) is semidet :- I =< Hi ),
plus(1)).

-%
------------------------------------------------------------------------
----
%
-%
------------------------------------------------------------------------
----
%
+%----------------------------------------------------------------------
--------%
+
+list__det_map2(_, [], [])    = [].
+
+list__det_map2(_, [], [_|_]) = _ :- error("list__det_map2: different
lengths").
+
+list__det_map2(_, [_|_], []) = _ :- error("list__det_map2: different
lengths").
+
+list__det_map2(Fn, [A | As], [B | Bs]) =
+       [Fn(A, B) | list__det_map2(Fn, As, Bs)].
+
+%----------------------------------------------------------------------
--------%
+%----------------------------------------------------------------------
--------%
Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.206
diff -u -r1.206 NEWS
--- NEWS        2001/03/27 05:23:01     1.206
+++ NEWS        2001/04/04 16:57:33
@@ -142,6 +142,8 @@

 Changes to the standard library:

+* We've added new functions list__cons/2 and list__det_map2/3.
+
 * We've added new predicates map__foldl2, tree234__foldl2 and
   std_util__aggregate2, and builtin__promise_only_solution_io.
--------------------------------------------------------------------------
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