如何在列表理解中使用字符串而不是 char

How can I use a string instead of a char in list comprehension

现在我正在尝试创建一个函数来查看输入 item 是否已经在 list 中,这是我的代码。

list :: [String]
list = ["a","b"]

listCheck :: String -> [String]
listCheck item = [(x:xs)| (x:xs) <- list, x == item]

当前的问题是它只能在输入是字符而不是字符串时进行过滤,当我未声明类型时 Haskell 使函数 Char -> [String] 运行良好,当它不被包含时它返回一个空列表,如果它被包含它返回一个包含该项目的列表。当我添加 String -> [String] 时,它返回了这个错误。

Couldn't match type `[Char]' with `Char'
      Expected: Char
      Actual: String

我试图将其更改为 x <- item 并且它编译了,但每次都只给出 [item, item]。我想知道我可以做些什么来让它使用字符串而不是字符,谢谢。

这部分将 x 转换为字符。

(x:xs) <- list

每个元素映射如

["a","b"]

'a':[] => x = 'a', xs = []
'b':[] => x = 'b', xs = []

所以,如果你想检查列表中的元素并找到一个,下面怎么样。

listCheck :: String -> [String]
listCheck item = [x| x <- list, x == item]
Prelude> listCheck "a"
["a"]
Prelude> listCheck "b"
["b"]
Prelude> listCheck "c"
[]
Prelude>