[m-dev.] for review again: Additions to list module.

Peter David ROSS petdr at cs.mu.OZ.AU
Wed Jun 24 10:25:38 AEST 1998


On 23-Jun-1998, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 23-Jun-1998, Peter David ROSS <petdr at cs.mu.OZ.AU> wrote:
> > +	% list__replace_nth(List0, N, R, List) is true iff List is List0 
> > +	% with Nth element replaced with R.
> > +	% Aborts if N < 1 and fails if length of List0 < N.
> > +	% (Position numbers start from 1.)
> > +	%
> > +:- pred list__replace_nth(list(T), int, T, list(T)).
> > +:- mode list__replace_nth(in, in, in, out) is semidet.
> 
> I think it would be better to do the same thing in the N < 1 and N > length
> cases -- either fail in both cases, or abort in both cases.
> 
I disagree, if N < 1 it is always an error.  This is not true for N >= 1.

> In fact it would make sense to have two versions, one that fails,
> and the other (with a `_det' suffix on the name) that aborts.
>
However I was uncomfortable with the error/fail pair and I this is
better in the long run.

Besides arguing about these silly things is better then documenting my
code.

Pete.

Index: list.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/list.m,v
retrieving revision 1.82
diff -u -r1.82 list.m
--- list.m	1998/04/08 14:45:42	1.82
+++ list.m	1998/06/24 00:23:12
@@ -229,6 +229,22 @@
 :- pred list__replace_all(list(T), T, T, list(T)).
 :- mode list__replace_all(in, in, in, out) is det.
 
+	% list__replace_nth(List0, N, R, List) is true iff List is List0 
+	% with Nth element replaced with R.
+	% Fails if N < 1 or if length of List0 < N.
+	% (Position numbers start from 1.)
+	%
+:- pred list__replace_nth(list(T), int, T, list(T)).
+:- mode list__replace_nth(in, in, in, out) is semidet.
+
+	% list__replace_nth_det(List0, N, R, List) is true iff List is List0 
+	% with Nth element replaced with R.
+	% Aborts if N < 1 or if length of List0 < N.
+	% (Position numbers start from 1.)
+	%
+:- pred list__replace_nth_det(list(T), int, T, list(T)).
+:- mode list__replace_nth_det(in, in, in, out) is det.
+
 	% list__sort_and_remove_dups(List0, List):
 	%	List is List0 sorted with duplicates removed.
 	%
@@ -256,6 +272,7 @@
 
 	% list__nth_member_search(List, Elem, Position):
 	%	Elem is the Position'th member of List.
+	% 	(Position numbers start from 1.)
 	%
 :- pred list__nth_member_search(list(T), T, int).
 :- mode list__nth_member_search(in, in, out) is semidet.
@@ -583,6 +600,40 @@
 	;
 		L = [X | L0],
 		list__replace_all(Xs, Y, Z, L0)
+	).
+
+list__replace_nth(Xs, P, R, L) :-
+	P > 0,
+	list__replace_nth_2(Xs, P, R, L).
+
+list__replace_nth_det(Xs, P, R, L) :-
+	(
+		P > 0
+	->
+		(
+			list__replace_nth_2(Xs, P, R, L0)
+		->
+			L = L0
+		;
+			error("list__replace_nth_det: Can't replace element whose index position is past the end of the list")
+		)
+	;
+		error("list__replace_nth_det: Can't replace element whose index position is less then 1.")
+	).
+
+
+:- pred list__replace_nth_2(list(T), int, T, list(T)).
+:- mode list__replace_nth_2(in, in, in, out) is semidet.
+
+list__replace_nth_2([X | Xs], P, R, L) :-
+	(
+		P = 1 
+	->
+		L = [R | Xs]
+	;
+		P1 is P - 1,
+		list__replace_nth(Xs, P1, R, L0),
+		L = [X | L0]
 	).
 
 %-----------------------------------------------------------------------------%



More information about the developers mailing list