Haskell filtering a nested list with data constructors

后端 未结 1 504
执笔经年
执笔经年 2021-01-29 14:48

Let\'s say I have the following data type

data Number = Positive Integer | Negative Integer
     deriving (Eq, Show)

I have a function definiti

相关标签:
1条回答
  • 2021-01-29 15:05

    One way is pattern matching on the nested list to peek at the first element of the first list to figure out what you need to filter:

    -- Note you could give this the more general type Eq a => [[a]] -> [[a]]
    -- (as well as a more appropriate name)
    removePos :: [[Number]] -> [[Number]]
    removePos [] = [] -- Empty list case.
    removePos xss@[[]:_] = xss -- If the first inner list is empty,
                               -- return the whole thing unchanged
    removePos ((x:_):xss) = filter (notElem x) xss
    
    0 讨论(0)
提交回复
热议问题