Haskell 函数,接受元素列表,如果列表包含重复项,则返回 true,如果不包含重复项,则返回

时间:2021-05-13 17:35:14

标签: haskell

我正在尝试创建一个 Haskell 函数,该函数接受一个元素列表,如果列表包含重复项,则返回 true,否则返回 false。

这是我所拥有的...我收到了一个错误,重复项有不同数量的参数

duplicates :: Eq a => [a] -> Bool
duplicates _ []= False
duplicates (x:xs) = callback n xs
where callback  = if x == n then elem else duplicates

duplicatesTests = [
                 not $ duplicates [1..10]
                ,duplicates "ABCAD"
                ,not $ duplicates "BAD"
                ,duplicates [1,2,1]
              ]

1 个答案:

答案 0 :(得分:2)

一定是

duplicates :: Eq a => [a] -> Bool
duplicates [] = False
duplicates (x:xs) = callback n xs
    where        -- callback       = if x == n then elem else duplicates
                    callback n xs  = if x == n then elem else duplicates

n 必须作为 callback 的参数出现,以便您能够在 callback 的正文中引用它。

在对 callback 的调用中,n 超出了范围。换句话说,它没有在任何地方定义。

一般来说,您使用的任何名称都必须来自某处

相关问题