[m-rev.] for review: Add det_take to the list module

Julien Fischer jfischer at opturion.com
Mon Dec 23 17:26:32 AEDT 2013



On Sat, 21 Dec 2013, Paul Bone wrote:

> Branches: master
>
> For review by anyone
>
> Add det_take to the list module
>
> The list module currently contains take, drop and det_drop predicates, but
> no det_take predicate.  Developers could use det_split_list instead, however
> det_take is more straight-forward and can be expected to exist since
> det_drop exists.
>
> library/list.m:
>    As above.
>
> NEWS:
>    Announce the new predicate.
> ---


> diff --git a/library/list.m b/library/list.m
> index d3f1856..8298ece 100644
> --- a/library/list.m
> +++ b/library/list.m
> @@ -232,12 +232,19 @@
> :- pred list.split_upto(int::in, list(T)::in, list(T)::out, list(T)::out)
>     is det.
>
> -    % list.take(Len, List, Start):
> +    % take(Len, List, Start):

Any reason you deleted the module qualifier there?  The style used in
the rest of this module is to have the qualifier present.  (I wouldn't
go around changing it unless you change the rest of the module to
maintain consistency -- better still, since this not consistent across
all the stdlib modules would be to extend the ``Standard Library'' bit
of the coding standard first ...)

>     % `Start' is the first `Len' elements of `List'. Fails if `List' has
>     % less than `Len' elements. See also: list.split_list.
>     %
> -:- pred list.take(int::in, list(T)::in, list(T)::out) is semidet.
> +:- pred take(int::in, list(T)::in, list(T)::out) is semidet.

Ditto.

> +    % det_take(Len, List, Start):
> +    %
> +    % As above.  Rather than failing when there are fewer than Len items in
> +    % List det_take will throw an exception.

This can just be:

     As above, but throw an exception instead of failing.

> +    %
> +:- pred det_take(int::in, list(T)::in, list(T)::out) is det.
>
>     % list.take_upto(Len, List, Start):
>     %
> @@ -2231,13 +2238,20 @@ list.split_upto(N, List, Start, End) :-
>         End = List
>     ).
>
> -list.take(N, As, Bs) :-
> +take(N, Xs0, Xs) :-
>     ( N > 0 ->
> -        As = [A | As1],
> -        list.take(N - 1, As1, Bs1),
> -        Bs = [A | Bs1]
> +        Xs0 = [X | Xs1],
> +        take(N - 1, Xs1, Xs2),
> +        Xs = [X | Xs2]
> +    ;
> +        Xs = []
> +    ).
> +
> +det_take(N, As, Bs) :-
> +    ( take(N, As, BsPrime) ->
> +        Bs = BsPrime
>     ;
> -        Bs = []
> +        unexpected($file, $pred, "Unexpected end of list")

A better message would be the same one that det_drop generates,

    "not enough elements"

That looks fine otherwise.

Cheers,
Julien.



More information about the reviews mailing list