[m-rev.] New library module: table.m

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Jan 21 17:37:18 AEDT 2003


On 21-Jan-2003, Ralph Becket <rafe at cs.mu.OZ.AU> wrote:
> Added a new library module, table.m, implementing 2d rectangular arrays.

Hmm.  Would it be better to name this `array2d', or `matrix',
rather than `table'?

I think I prefer the name `array2d'.

> tests/hard_coded/test_table.m:
> tests/hard_coded/test_table.exp:
> tests/hard_coded/Mmakefile:
> 	Test case added.

The test case should IMHO also test that the implementation does bounds
checking.

> Index: library/table.m
> +    % table([[X11, ..., X1N], ..., [XM1, ..., XMN]]) constructs a table
> +    % of size M * N.
...
> +    % new(M, N, X) = table([[X11, ..., X1N], ..., [XM1, ..., XMN]])
> +    % where each XIJ = X.
...
> +    % table([[X11, ..., X1N], ..., [XM1, ..., XMN]]) ^ elem(I, J) = XIJ
> +    %
> +    % An exception is thrown unless 0 =< I < M, 0 =< J < N.

Do table indexes start at zero, or at 1?

The documentation for `elem' is contradictory on this point.
The use of "X11, ..., X1N" in the documentation of `table',
`new', and `elem' strongly suggests that indexes start at 1.
And the definition of elem(I, J) = XIJ confirms it.
But then the bit about when exceptions get thrown contradicts this.

> +    % table(Rows, Cols, Array)
> +    %
> +:- type table(T) ---> table(int, int, array(T)).

You should document here whether the table is stored in
row-major or column-major order.

> +table(Xss @ [Xs | _]) = T :-
> +
> +    M = length(Xss),
> +    N = length(Xs),
> +    T = table(M, N, A @ array(condense(Xss))),
> +
> +    ( size(A) \= M * N =>
> +        error("table.table/1: non-rectangular list of lists") ).

The error condition might not get checked if this code is compiled
with --no-fully-strict, which would contravene the documentation for
this function.  So either the code should be rewritten so that it
only binds T in the case when size(A) = M * N, or you should add a comment
here and also in the Mmakefile, where it passes `--strict-sequential',
saying that this code relies on it.  I recommend the former.

Also, IMHO the second use of @ in this procedure does not improve
readability.

So I would write it as

	table(Xss @ [Xs | _]) = T :-
	    M = list.length(Xss),
	    N = list.length(Xs),
	    A = array(list.condense(Xss))
	    ( array.size(A) = M * N =>
		T = table(M, N, A)
	    ;
		error("table.table/1: non-rectangular list of lists")
	    ).

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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