[m-rev.] diff: updates to random modules

Mark Brown mark at mercurylang.org
Fri Sep 6 19:39:49 AEST 2019


Hi,

On Fri, Sep 6, 2019 at 7:32 PM Julien Fischer <jfischer at opturion.com> wrote:
>
>
> Hi Mark,
>
> On Fri, 6 Sep 2019, Mark Brown wrote:
>
> > This addresses the discussion on the users list.
>
> That looks fine, with the exception of the comment below.
>
> > diff --git a/library/random.m b/library/random.m
> > index 3b9873f..98322ca 100644
> > --- a/library/random.m
> > +++ b/library/random.m
>
> ...
>
> > @@ -511,6 +535,30 @@ normal_floats(U, V, !R) :-
> >          normal_floats(U, V, !R)
> >      ).
> >
> > +shuffle_list(L0, L, !R) :-
> > +    A0 = array(L0),
> > +    shuffle_array(A0, A, !R),
> > +    L = array.to_list(A).
> > +
> > +shuffle_array(A0, A, !R) :-
> > +    Lo = array.min(A0),
> > +    Hi = array.max(A0),
> > +    Sz = array.size(A0),
> > +    shuffle_2(Lo, Lo, Hi, Sz, A0, A, !R).
> > +
> > +:- pred shuffle_2(int::in, int::in, int::in, int::in,
> > +    array(T)::array_di, array(T)::array_uo, R::in, R::out) is det
> > +    <= random(R).
> > +
> > +shuffle_2(I, Lo, Hi, Sz, !A, !R) :-
> > +    ( if I > Hi then
> > +        true
> > +    else
> > +        uniform_int_in_range(Lo, Sz, J, !R),
> > +        swap_elems(I, J, !A),
>
> The array module now exports array.swap/4 and array.unsafe_swap/4, the
> latter of which you should be using here and in the unique version.
> swap_elems/4 can be deleted.

Thanks, I will do so.

Mark


More information about the reviews mailing list