请允许我指出,我有一套关系,似乎这样:
relations :: [(A, B)]
instance Monoid A
instance Monoid B
我想将这套关系转变为一套新的<代码>关系。 A和。 B
。
现在,这里的trick脚:是:
A
s that are equal should have theirB
smappend
ed.B
s that are equal should have theirA
smappend
ed.- Repeat until all
A
s andB
s are distinct (Or don t, depending on if this can be done non-iteratively somehow).
www.un.org/Depts/DGACM/index_spanish.htm EDIT: 令状的制约使问题变得微不足道,因此我删除了这个问题。
可以假定,<代码>Ord,Hashable
,或您需要的任何其他东西。 就所有意图和目的而言,人们可以说,A
>s behaves exactly<>em> /em>,如HashSet
和B
behavesexactly,如Vector
(或其他一些具有合理规模检查的类型)。
This means that one can assume that let s = size (mappend a b); s >= size a; s >= size b
, and that a, b :: B; mappend a b /= mappend b a <=> a, b not mempty; a > b => (mappend a c) > b
, etc.
An example of how this transformation would happen (Pretend that <a, b>
is Set.fromList [a, b]
)
[(<1>, [a]), (<4>, [d]), (<2>, [b]), (<5>, [e]), (<1>, [b]), (<2>, [a]), (<3>, [c])]
-- Merge `A`s for equal `B`s
[(<1,2>, [a]), (<4>, [d]), (<1,2> [b]), (<5>, [e]), (<3>, [c])]
-- Merge `B`s for equal `A`s
[(<1,2>, [a, b]), (<4>, [d]), (<5>, [e]), (<3>, [c])]
-- All values are distinct, so we re done.
如何以尽可能高效率的方式(时间、空间)做到这一点?