识别 Haskell 元组中的重复项

Identifying duplicates in Haskell tuples

我正在尝试编写一个函数,如果元组中的任何两个值相同,它将 Nothing 一个 Just Int 元组。对于五个值的元组,这就是我所拥有的。显然,还有改进的余地:

nothingIfMatch :: Maybe (Int, Int, Int, Int, Int) -> Maybe (Int, Int, Int, Int, Int)
nothingIfMatch Nothing = Nothing
nothingIfMatch (Just (a, b, c, d, e))
    | a == b = Nothing
    | a == c = Nothing
    | a == d = Nothing
    | a == e = Nothing
    | b == c = Nothing
    | b == d = Nothing
    | b == e = Nothing
    | c == d = Nothing
    | c == e = Nothing
    | d == e = Nothing
    | otherwise = Just (a, b, c, d, e)

考虑到一个 n 元组有 "n choose 2" 个可能的交集,在这种情况下,只有 10 个选项。但是想象这是一个 8 元组,有 28 种可能性,或者一个 10 元组,有 45 种可能性。

必须有更惯用的方法来做到这一点,可能依赖于非确定性特征。

应该怎么做?

我们可以先生成一个 Int 的列表,然后执行所有相等性检查:

import Data.List(tails)

twoEqual :: Eq a => [a] -> Bool
twoEqual xs = any (uncurry elem) [(h, t) | (h:t) <- tails xs]

这里我们首先为列表中的每个元素生成一个包含元素和列表的rest的元组。然后我们执行 elem 函数:我们在项目和列表的其余部分上调用 elem 如果这些检查中的 any 成立,那么我们 return True, False 否则。

所以现在我们可以从这个元组构造一个列表,然后使用守卫来执行检查:

nothingIfMatch :: Eq a => Maybe (a, a, a, a, a) -> Maybe (a, a, a, a, a)
nothingIfMatch = (>>= f)
    where f r@(a, b, c, d, e) | twoEqual [a, b, c, d, e] = Nothing
                              | otherwise = Just r

我们可以轻松地向元组添加一个额外的元素,并在 twoEqual 调用中将其添加到列表中。这里我们还是执行O(n2)。如果我们可以先对元素排序,我们可以在 O(n log n) 中完成,或者我们甚至可以在 O(n) 中完成如果元素是 hashable 并且没有哈希冲突发生。

例如:

-- O(n log n) if the elements can be ordered

import Data.List(sort, tails)

twoEqual :: Ord a => [a] -> Bool
twoEqual xs = or [h1 == h2 | (h1:h2:_) <- tails (sort xs)]

或者如果元素可以散列:

-- O(n) in case the elements are hashable and no hash collisions

import Data.Hashable(Hashable)
import Data.HashSet(fromList, member)

twoEqual :: (Hashable a, Ord a) => [a] -> Bool
twoEqual xs = any (flip member hs) xs
    where hs = fromList xs