Haskell分区与集合-解析错误?

Haskell分区与集合-解析错误?,haskell,Haskell,分区的形式必须为: 分区:a->Bool->Set a->Set a,Set a 我的代码: partition pred [] = [] partition pred (front : rest) = partition pred rest, if pred front = (front : rest), otherwise 但是,我得到一个错误: 错误:在输入“,”上分析错误 | 192 |=分区pred rest,如果pred front 如果可能,任何人都可以告

分区的形式必须为: 分区:a->Bool->Set a->Set a,Set a

我的代码:

partition pred [] = []
partition pred (front : rest)
      = partition pred rest, if pred front
      = (front : rest), otherwise
但是,我得到一个错误: 错误:在输入“,”上分析错误 | 192 |=分区pred rest,如果pred front


如果可能,任何人都可以告诉我修复方法?

这里有几个问题:

首先,您使用的语法有点类似于guards,但不是正确的语法; 类型不匹配,您的函数应该返回两个元组的集合,因此需要将结果包装为两个元组。乍一看,您的实现似乎更多地与过滤器相关,而不是与分区相关;和 项目应该是集合,而不是列表。 对于列表,分区函数如下所示:

partition :: (a -> Bool) -> a -> ([a], [a])
partition pred [] = ([], [])
partition pred (x:xs)
    | pred x = ((x:y1), y2)
    | otherwise = (y1, (x:y2))
    where (y1, y2) = partition pred xs
我把上面的代码转换成一个集合作为练习

这是无效的Haskell。你从哪里学来的?停止使用那个资源。防护装置应类似于:

| pred front = partition pred rest
但是,正如在评论中所指出的,这些代码不会做你想做的事情——你把它扔掉了。就这一点而言,前方是什么?让我看看

(front : rest)
这根本不是一套,只是一张单子。好吧,在你的世界里,集合就是列表。但是,您需要在与谓词匹配的集合中放在前面,该谓词是您将从分区pred rest得到的答案的一部分


有更多的类型错误,但希望这能让您继续。

除非您有一些特殊的扩展,否则这不是guards的样子。此外,类型不匹配,因为您的函数应该返回两个元组的集合。
(front : rest)