Haskell - 解析错误/使用多个 where 子句

发布于 2024-12-23 18:54:37 字数 524 浏览 0 评论 0原文

当尝试定义一个函数来删除集合 m 的最大子集(同时也是集合 a 中集合 a 的子集)时,我遇到了以下错误:

filename.hs:7:33:parse error (possibly incorrect indentation)

对于以下代码:

exclude :: Integral t => [t] -> [t] -> [t]
a `exclude` m
           | m == [] = a
           | a == (b ++ c) = b
           | otherwise = []
           where b /= []
           where c = [z | z <- m]

如何实现多个条件/定义(使用 where 或其他方式),或更正函数以以不同的方式正常工作?

when trying to define a function that would remove the largest subset of set m that is also a subset of set a from set a, I encountered the following error:

filename.hs:7:33:parse error (possibly incorrect indentation)

for the following code:

exclude :: Integral t => [t] -> [t] -> [t]
a `exclude` m
           | m == [] = a
           | a == (b ++ c) = b
           | otherwise = []
           where b /= []
           where c = [z | z <- m]

how do I implement multiple conditions/definitions (using where or otherwise), or correct the function to properly work in a different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亽野灬性zι浪 2024-12-30 18:54:38

你的问题的一部分很容易回答。您可以在一个 where 子句中拥有多个定义,例如

foo n
    | even r = bar
    | s < 12 = baz
    | otherwise = quux
      where
        r = n `mod` 1357
        h = a + b
          where
            (a,b) = r `divMod` 53    -- nested where-clause
        s = r - 3*h

,您可以嵌套 where 子句。但在 where 子句中,只能有定义。条件将进入守卫(或右侧的 if then else 表达式),并且可以与布尔运算符 (&&)组合>(||)not ...

至于你的代码,到目前为止我还没有弄清楚你打算做什么。

One part of your question is easily answerable. You can have multiple definitions in one where clause, as in

foo n
    | even r = bar
    | s < 12 = baz
    | otherwise = quux
      where
        r = n `mod` 1357
        h = a + b
          where
            (a,b) = r `divMod` 53    -- nested where-clause
        s = r - 3*h

and you can have nested where-clauses. But in a where-clause, you can only have definitions. Conditions would go into the guards (or if then else expressions on the right hand side) and can be combined with the boolean operators, (&&), (||), not ...

As for your code, so far I haven't figured out what you intended it to do.

在你怀里撒娇 2024-12-30 18:54:38

说“集合m的最大子集也是集合a的子集”
与“m 的所有元素也是 a 的元素”相同。

那么问题的解决方案简单地表述为:

exclude a = filter (`notElem` a)

当应用于 m 时,将为您提供 m 对任何元素取模的子集
它们也是 a 的成员。也就是说,它将“删除最大的子集
m 也是 a 的子集”。

Saying "the largest subset of set m that is also a subset of set a"
is the same as saying "all elements of m that are also elements of a".

Then the solution to your problem is stated simply as:

exclude a = filter (`notElem` a)

which when applied to m will give you a subset of m modulo any elements
that are also members of a. That is, it will "remove the largest subset of
m that is also a subset of a".

眸中客 2024-12-30 18:54:38

事实上,Data.List和Data.Set中都有一个函数叫做“\”。我将展示 Data.List 的 '\' 函数。

import Data.List
exclude :: Integral t => [t] -> [t] -> [t]
a `exclude` m = a\\m

In fact,there is a function in Data.List and Data.Set called '\'. I'll show '\' function of Data.List .

import Data.List
exclude :: Integral t => [t] -> [t] -> [t]
a `exclude` m = a\\m
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文