[m-rev.] for review: add predicate versions of int.fold_up/down

Julien Fischer juliensf at students.cs.mu.OZ.AU
Fri Mar 26 17:14:38 AEDT 2004


Estimated hours taken: 1
Branches: main

Add predicate versions of int.fold_up and int.fold_down.

XXX We should probably consider swapping the order of
the arguments in the function versions so they correspond
to the predicate versions more closely.

library/int.m:
	Add the predicates int.fold_up/5 and int.fold_down/5.

tests/hard_coded/int_fold_up_down.m:
tests/hard_coded/int_fold_up_down.exp:
	Test the predicate versions as well.

NEWS:
	Mention the new predicates.

Julien.

Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.330
diff -u -r1.330 NEWS
--- NEWS	19 Mar 2004 14:33:36 -0000	1.330
+++ NEWS	26 Mar 2004 06:05:35 -0000
@@ -91,8 +91,9 @@

 Changes to the Mercury standard library:

-* We've added two new functions, fold_up/4 and fold_down/4, to int.m.
-  These functions support iteration over contiguous integer ranges.
+* We've add some new predicates and functions to int.m.
+  int.fold_up/4, int.fold_down/4, int.fold_up/5, int.fold_down/5
+  support iteration over contiguous integer ranges.

 * We've added a new library module, `array2d', for two-dimensional arrays.

Index: library/int.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/int.m,v
retrieving revision 1.98
diff -u -r1.98 int.m
--- library/int.m	2 Feb 2004 03:53:07 -0000	1.98
+++ library/int.m	26 Mar 2004 00:34:35 -0000
@@ -233,15 +233,31 @@
 :- func int__bits_per_int = int.
 :- pred int__bits_per_int(int::out) is det.

+	% fold_up(F, Low, High, !Acc) <=> list.foldl(F, Low `..` High, !Acc)
+	%
+	% NOTE: fold_up/5 is undefined if High = int.max_int.
+	%
+:- pred int__fold_up(pred(int, T, T), int, int, T, T).
+:- mode int__fold_up(pred(in, in, out) is det, in, in, in, out) is det.
+:- mode int__fold_up(pred(in, di, uo) is det, in, in, di, uo) is det.
+
 	% fold_up(F, Acc, Low, High) = list.foldl(F, Low `..` High, Acc)
 	%
-	% NOTE: fold_up/4 is undefined if High = int__max_int.
+	% NOTE: fold_up/4 is undefined if High = int.max_int.
 	%
 :- func int__fold_up(func(int, T) = T, T, int, int) = T.

+	% fold_down(F, Low, High, !Acc) <=> list.foldr(F, Low `..` High, !Acc)
+	%
+	% NOTE: fold_down/5 is undefined if Low int.min_int.
+	%
+:- pred int__fold_down(pred(int, T, T), int, int, T, T).
+:- mode int__fold_down(pred(in, in, out) is det, in, in, in, out) is det.
+:- mode int__fold_down(pred(in, di, uo) is det, in, in, di, uo) is det.
+
 	% fold_down(F, Acc, Low, High) = list.foldr(F, Low `..` High, Acc)
 	%
-	% NOTE: fold_down/4 is undefined if Low = int__min_int.
+	% NOTE: fold_down/4 is undefined if Low = int.min_int.
 	%
 :- func int__fold_down(func(int, T) = T, T, int, int) = T.

@@ -674,10 +690,22 @@

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

+int__fold_up(F, Lo, Hi, !A) :-
+	( if	Lo =< Hi
+	  then	F(Lo, !A), int__fold_up(F, Lo + 1, Hi, !A)
+	  else	true
+	).
+
 int__fold_up(F, A, Lo, Hi) =
 	( if Lo =< Hi then int__fold_up(F, F(Lo, A), Lo + 1, Hi) else A ).

 %-----------------------------------------------------------------------------%
+
+int__fold_down(F, Lo, Hi, !A) :-
+	( if 	Lo =< Hi
+	  then	F(Hi, !A), int__fold_down(F, Lo, Hi - 1, !A)
+	  else	true
+	).

 int__fold_down(F, A, Lo, Hi) =
 	( if Lo =< Hi then int__fold_down(F, F(Hi, A), Lo, Hi - 1) else A ).
Index: tests/hard_coded/int_fold_up_down.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/int_fold_up_down.exp,v
retrieving revision 1.1
diff -u -r1.1 int_fold_up_down.exp
--- tests/hard_coded/int_fold_up_down.exp	27 Jan 2004 03:26:38 -0000	1.1
+++ tests/hard_coded/int_fold_up_down.exp	26 Mar 2004 05:18:09 -0000
@@ -1,2 +1,4 @@
 (5(4(3(2(1)))))
 (1(2(3(4(5)))))
+(5(4(3(2(1)))))
+(1(2(3(4(5)))))
Index: tests/hard_coded/int_fold_up_down.m
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/int_fold_up_down.m,v
retrieving revision 1.1
diff -u -r1.1 int_fold_up_down.m
--- tests/hard_coded/int_fold_up_down.m	27 Jan 2004 03:26:38 -0000	1.1
+++ tests/hard_coded/int_fold_up_down.m	26 Mar 2004 05:16:30 -0000
@@ -27,12 +27,20 @@

 main(!IO) :-
     io.write_string(int.fold_up(f, "", 1, 5)   ++ "\n", !IO),
-    io.write_string(int.fold_down(f, "", 1, 5) ++ "\n", !IO).
-
+    io.write_string(int.fold_down(f, "", 1, 5) ++ "\n", !IO),
+    int.fold_up(p, 1, 5, "", FoldUpRes),
+    io.write_string(FoldUpRes ++ "\n", !IO),
+    int.fold_down(p, 1, 5, "", FoldDownRes),
+    io.write_string(FoldDownRes ++ "\n", !IO).

 :- func f(int, string) = string.

 f(X, S) = string.format("(%d%s)", [i(X), s(S)]).
+
+
+:- pred p(int::in, string::in, string::out) is det.
+
+p(X, S, string.format("(%d%s)", [i(X), s(S)])).

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

--------------------------------------------------------------------------
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