[m-users.] Cartesian product of two sets of things.

Volker Wysk post at volker-wysk.de
Mon Oct 9 20:04:44 AEDT 2023


Am Montag, dem 09.10.2023 um 09:09 +0100 schrieb Sean Charles
(emacstheviking):
> My question then is, given the two loops are basically generating a
> cartesian product, is there a module function somewhere that would do that
> for me, generate a single list of all pairs? Then I could list.chunk and
> maybe produce a cleaner bit of code. I am currently also considering on
> uncommenting out the set_hit() typeclass predicate so that I can modify
> the objects in place, so instead of returning two sets of objects ID,s I'd
> be returning a new set of objects with their respective hit flags set.

I don't know if there is, but here's my version:

-----snip-----
:- pred cart_prod(set(T)::in, set(U)::in, set(pair(T, U))::out) is det.

cart_prod(Set1, Set2, Res) :-
    solutions(
        (pred((A - B)::out) is nondet :-
            set.member(A, Set1),
            set.member(B, Set2)
        ),
        Res).
-----snip-----

Cheers,
Volker


More information about the users mailing list