如何将多个元素放入“elem”
How can I put multiple elements into ‘elem’
假设我想做类似"ace" `elem` "abcdefg"
的事情。当然我们不能,因为您需要第一个元素类型的列表。相反,有没有一种方法可以把它变成获取“ace”中的每个字符并将它们单独与“abcdefg”进行比较,然后使用或语句将它们组合起来,但无需手动执行此操作(因为在我的程序中它是一个未知长度的变量和字符)。
这是一个更普遍问题的特定案例:您有一个函数 Char -> Bool
(或 a -> Bool
),并且您希望将其改编为函数 [a] -> Bool
。也就是说,你需要的是一个函数
adaptOr :: (a -> Bool) -> ([a] -> Bool)
好吧,这实际上是您可以做到的 ask Hoogle about!第一次点击是
any :: (a -> Bool) -> [a] -> Bool
base GHC.List GHC.OldList
Applied to a predicate and a list, any determines if any element of the list satisfies the predicate. For the result to be False, the list must be finite; True, however, results from a True value for the predicate applied to an element at a finite index of a finite or infinite list.
>>> any (> 3) []
False
>>> any (> 3) [1,2]
False
>>> any (> 3) [1,2,3,4,5]
True
>>> any (> 3) [1..]
True
>>> any (> 3) [0, -1..]
* Hangs forever *
all :: (a -> Bool) -> [a] -> Bool
base GHC.List GHC.OldList
Applied to a predicate and a list, all determines if all elements of the list satisfy the predicate. For the result to be True, the list must be finite; False, however, results from a False value for the predicate applied to an element at a finite index of a finite or infinite list.
>>> all (> 3) []
True
>>> all (> 3) [1,2]
False
>>> all (> 3) [1,2,3,4,5]
False
>>> all (> 3) [1..]
False
>>> all (> 3) [4..]
* Hangs forever *
现在您需要考虑哪一个适合您的情况,然后相应地使用它。你要
all⁄any (\c -> c`elem`"abcdefg") "ace"
这也可以用中缀部分来写:
all⁄any (`elem`"abcdefg") "ace"
根据需要使用 or
语句:
or [c `elem` "abcdefg" | c <- "ace"]
这个怎么想出来的?
你已经知道你想要
'a' `elem` "abcdefg" ||
'c' `elem` "abcdefg" ||
'e' `elem` "abcdefg"
(||)
的类型是 Bool -> Bool -> Bool
.
但是您有一个 list 字符要测试。在您测试了它们中的每一个之后,您将获得 list 的布尔结果。因此,您需要一个 [Bool] -> Bool
.
类型的函数
假设我想做类似"ace" `elem` "abcdefg"
的事情。当然我们不能,因为您需要第一个元素类型的列表。相反,有没有一种方法可以把它变成获取“ace”中的每个字符并将它们单独与“abcdefg”进行比较,然后使用或语句将它们组合起来,但无需手动执行此操作(因为在我的程序中它是一个未知长度的变量和字符)。
这是一个更普遍问题的特定案例:您有一个函数 Char -> Bool
(或 a -> Bool
),并且您希望将其改编为函数 [a] -> Bool
。也就是说,你需要的是一个函数
adaptOr :: (a -> Bool) -> ([a] -> Bool)
好吧,这实际上是您可以做到的 ask Hoogle about!第一次点击是
any :: (a -> Bool) -> [a] -> Bool
base GHC.List GHC.OldList
Applied to a predicate and a list, any determines if any element of the list satisfies the predicate. For the result to be False, the list must be finite; True, however, results from a True value for the predicate applied to an element at a finite index of a finite or infinite list.
>>> any (> 3) []
False
>>> any (> 3) [1,2]
False
>>> any (> 3) [1,2,3,4,5]
True
>>> any (> 3) [1..]
True
>>> any (> 3) [0, -1..]
* Hangs forever *
all :: (a -> Bool) -> [a] -> Bool
base GHC.List GHC.OldList
Applied to a predicate and a list, all determines if all elements of the list satisfy the predicate. For the result to be True, the list must be finite; False, however, results from a False value for the predicate applied to an element at a finite index of a finite or infinite list.
>>> all (> 3) []
True
>>> all (> 3) [1,2]
False
>>> all (> 3) [1,2,3,4,5]
False
>>> all (> 3) [1..]
False
>>> all (> 3) [4..]
* Hangs forever *
现在您需要考虑哪一个适合您的情况,然后相应地使用它。你要
all⁄any (\c -> c`elem`"abcdefg") "ace"
这也可以用中缀部分来写:
all⁄any (`elem`"abcdefg") "ace"
根据需要使用 or
语句:
or [c `elem` "abcdefg" | c <- "ace"]
这个怎么想出来的?
你已经知道你想要
'a' `elem` "abcdefg" ||
'c' `elem` "abcdefg" ||
'e' `elem` "abcdefg"
(||)
的类型是 Bool -> Bool -> Bool
.
但是您有一个 list 字符要测试。在您测试了它们中的每一个之后,您将获得 list 的布尔结果。因此,您需要一个 [Bool] -> Bool
.