[m-dev.] for review: added two utility functions to list.m

Ralph Becket rbeck at microsoft.com
Thu Mar 1 03:19:32 AEDT 2001


Estimated hours taken: 0.25

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

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/02/28 15:41:30
@@ -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'.
@@ -355,6 +359,15 @@

 :- func list__zip(list(T), list(T)) = list(T).

+       % list__zip_with(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__zip_with(func(T1, T2) = T, list(T1), list(T2)) = list(T).
+
        % list__duplicate(Count, Elem, List) is true iff List is a list
        % containing Count duplicate copies of Elem.
        %
@@ -643,8 +656,12 @@
 
%---------------------------------------------------------------------------
--%

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

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

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

@@ -1420,6 +1437,17 @@

 Lo `..` Hi =
        list__series(Lo, ( pred(I::in) is semidet :- I =< Hi ), plus(1)).
+
+%--------------------------------------------------------------------------
----%
+
+list__zip_with(_, [], [])    = [].
+
+list__zip_with(_, [], [_|_]) = throw("list__zip_with: different lengths").
+
+list__zip_with(_, [_|_], []) = throw("list__zip_with: different lengths").
+
+list__zip_with(Fn, [A | As], [B | Bs]) =
+       [Fn(A, B) | list__zip_with(Fn, As, Bs)].

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

- Ralph
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list