简体   繁体   中英

Haskell - parse error/ using multiple where clauses

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?

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.

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 ".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM