这是一个用Haskell编写的代码,用于查找列表中第一个重复元素。如果你有两个像这样的列表:L1={1,2,3,4} L2={1,2,3,1},则第一个列表的结果应该是“未找到重复项”,而第二个列表的结果应该是整数1。如果一个列表为L3={1,2,1,3,3},答案仍然应该是1。
我尝试过了,但卡在了条件检查上: 我们将使用联合数据类型。
我尝试过了,但卡在了条件检查上: 我们将使用联合数据类型。
data Res a = DuplicateFound a | NoDuplicateFound
deriving (Show,Eq)
findDuplicate [] = NoDuplicateFound
findDuplicate (x:xs) = if (condition???)
where
aux x [] = NoDuplicateFound
aux x (y:ys) = if x == y then DuplicateFound x
else aux x ys
else NoDuplicateFound
我知道这是一个可悲的代码,请帮助我改进它。