Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell-检查列表中是否有重复的元素_Haskell - Fatal编程技术网

Haskell-检查列表中是否有重复的元素

Haskell-检查列表中是否有重复的元素,haskell,Haskell,这是一项我基本上已经完成的大学家庭作业,我不是来这里寻求答案的 给定的任务是查找给定的元素是否在列表中出现多次。我尝试的算法是创建将用作计数器的countdup,并计算在列表中找到该元素的次数。如果countDups大于1,则ismembertweeth(应返回Bool)将为True,否则为False 是的,我是Haskell的新手,所以如果这是一种完全可怕的实现方式,我很抱歉 countDups x [] = 0 countDups x (y:ys) | x == y = 1 +

这是一项我基本上已经完成的大学家庭作业,我不是来这里寻求答案的

给定的任务是查找给定的元素是否在列表中出现多次。我尝试的算法是创建将用作计数器的
countdup
,并计算在列表中找到该元素的次数。如果
countDups
大于1,则
ismembertweeth
(应返回
Bool
)将为
True
,否则为
False

是的,我是Haskell的新手,所以如果这是一种完全可怕的实现方式,我很抱歉

countDups x [] = 0 
countDups x (y:ys) 
    | x == y = 1 + countDups x ys 
    | otherwise = countDups x ys

isMemberTwice x [] = False      --base case; empty list
isMemberTwice x (y: ys) 
    | countDups > 1 = True 
    | otherwise False 
错误消息

    parse error (possibly incorrect indentation or mismatched brackets)
Failed, modules loaded: none.
由于下面的评论,我更新了,但仍然不工作-有什么建议吗

isMember _ [] = 0
isMember a (x:xs)
    | (a == x) = 1
    | otherwise isMember a xs

isMemberTwice _ [] = False
isMemberTwice a (x:xs)
    | (a == x) = if ((1 + isMember a (x:xs)) > 1) then True
    | otherwise isMemberTwice a xs
一些提示:

  • 暂时忘掉countDups;您不需要它来编写两次
    ismembers

  •  isMember x [] = ???
     isMember x (y : ys)
       | x == y = ???
       | otherwise = ???
    
     isMemberTwice x [] = ???
     isMemberTwice x (y : ys)
       | x == y = ???
       | otherwise = ???
    
  • 首先写
    isMember

  • 使用
    isMember
    写入
    isMember两次

     isMember x [] = ???
     isMember x (y : ys)
       | x == y = ???
       | otherwise = ???
    
     isMemberTwice x [] = ???
     isMemberTwice x (y : ys)
       | x == y = ???
       | otherwise = ???
    
  • 一些提示:

  • 暂时忘掉countDups;您不需要它来编写两次
    ismembers

  •  isMember x [] = ???
     isMember x (y : ys)
       | x == y = ???
       | otherwise = ???
    
     isMemberTwice x [] = ???
     isMemberTwice x (y : ys)
       | x == y = ???
       | otherwise = ???
    
  • 首先写
    isMember

  • 使用
    isMember
    写入
    isMember两次

     isMember x [] = ???
     isMember x (y : ys)
       | x == y = ???
       | otherwise = ???
    
     isMemberTwice x [] = ???
     isMemberTwice x (y : ys)
       | x == y = ???
       | otherwise = ???
    

  • 在最后一个
    False
    之前,您似乎缺少
    =
    。由于混乱的Stackoverflow降价,很难说是否也有任何缩进错误,但我猜在这种情况下没有。(不过,解析后还会出现其他错误。)是的,我添加了等号,得到了另一个错误。您对更好的算法有什么建议吗?每个
    if
    都需要
    else
    分支。我还想在每个函数之前添加类型注释
    isMember::…
    ,因为在Haskell中这样做是很常见的,无论是出于文档目的还是为了改进GHC类型的错误消息(如果您说明了需要什么,GHC可以发现差异)(即元素类型是
    Ord
    的一个实例)然后,您也可以考虑对列表进行排序,然后查看是否可以找到两个相等的后续元素。在上一个代码> false 之前,您似乎缺少了“代码>=/CODE”。(不过,一旦解析,还会出现其他错误。)是的,我添加了等号,得到了另一个错误。你对更好的算法有什么建议吗?每个
    if
    都需要一个
    else
    分支。我还需要在每个函数之前添加类型注释
    isMember::…
    ,因为在Haskell中这样做很常见,这既是为了文档目的,也是为了改善GHC类型错误混乱年龄(如果您陈述了需要什么,GHC可以发现差异)。如果列表中的元素可以排序(即元素类型是
    Ord
    的实例)然后你也可以考虑对列表进行排序,然后看看是否可以找到两个相等的元素。谢谢!而且根本不挑剔——只是好奇。这会产生一个O(n ^ 2)运行时间吗?@ A1YX,我只给了你一个模板。如果你填错了,你应该能够证明它执行O(n)。具有O(n)的相等性测试行政工作。你介意就编辑后的问题中的代码给出一些建议吗?我试着实现你的建议,但我肯定搞砸了somewhere@a1yx,您的
    isMember
    似乎出于某种原因返回了一个数字,而不是布尔值。它应该返回一个布尔值。您不需要任何数字来解决此问题。谢谢你!一点也不挑剔-只是好奇。这会产生O(N^2)运行时间吗?@a1yx,我只给了你一个模板。如果你填写正确,你应该能够证明它使用O(N)执行O(N)相等测试行政工作。你介意就编辑后的问题中的代码给出一些建议吗?我试着实现你的建议,但我肯定搞砸了somewhere@a1yx,您的
    isMember
    似乎出于某种原因返回了一个数字,而不是布尔值。它应该返回一个布尔值。您不需要任何数字来解决此问题。