[m-rev.] diff: add predicate versions of cord.{cons,snoc}

Julien Fischer jfischer at opturion.com
Tue Jun 7 23:52:45 AEST 2016


Add predicate versions of cord.{cons,snoc}.

library/cord.m:
      As above.

NEWS:
      Announce the additions.

Julien.

diff --git a/NEWS b/NEWS
index a28d121..42ac2da 100644
--- a/NEWS
+++ b/NEWS
@@ -330,10 +330,12 @@ Changes to the Mercury standard library:
    Existing code that uses these modes should replace their use with `in'
    or `out' respectively.

-* The following functions have been added to the cord module
+* The following predicates and functions have been added to the cord module:

     - to_list/1     (synonym for the existing list/1 function)
     - to_rev_list/1 (synonym for the existing rev_list/1 function)
+   - cons/3
+   - snoc/3

  * The following function has been added to the array2d module

diff --git a/library/cord.m b/library/cord.m
index a17b96a..2548702 100644
--- a/library/cord.m
+++ b/library/cord.m
@@ -94,11 +94,13 @@
      % An O(1) operation.
      %
  :- func cons(T, cord(T)) = cord(T).
+:- pred cons(T::in, cord(T)::in, cord(T)::out) is det.

      % list(snoc(C, X)) = list(C) ++ [X]
      % An O(1) operation.
      %
  :- func snoc(cord(T), T) = cord(T).
+:- pred snoc(T::in, cord(T)::in, cord(T)::out) is det.

      % list(CA ++ CB) = list(CA) ++ list(CB)
      % An O(1) operation.
@@ -361,6 +363,9 @@ cons(X, C) = XC :-
          XC = nonempty_cord(branch_node(unit_node(X), N))
      ).

+cons(X, !C) :-
+    !:C = cons(X, !.C).
+
  %---------------------------------------------------------------------------%

  snoc(C, X) = CX :-
@@ -372,6 +377,9 @@ snoc(C, X) = CX :-
          CX = nonempty_cord(branch_node(N, unit_node(X)))
      ).

+snoc(X, !C) :-
+    !:C = snoc(!.C, X).
+
  %---------------------------------------------------------------------------%

  A ++ B = C :-


More information about the reviews mailing list