現在我有這段代碼\
filter (any (=='a')) [String]
-- [String] is just any string list
如果你不能告訴這只是過濾字串中包含'a'的任何內容。我想知道的是如何搜索多個字符?如果我嘗試制作一個字串/[char],那么由于 any 函式采用的輸入,我會收到一個錯誤。我該如何解決這個問題,因為出于我的目的,我總是會有未知數量的字符,所以我需要一種方法來將多個字符輸入這個函式。謝謝你。
uj5u.com熱心網友回復:
您可以使用它elem來檢查某物是否是串列的元素。
filter (`elem` ['a','e','i','o','u']) "hello world!"
-- or
filter (`elem` "aeiou") "hello world!"
這等效于使用顯式 lambda:
filter
(\x -> x=='a' || x=='e' || x=='i' || x=='o' || x=='u')
"hello world!"
uj5u.com熱心網友回復:
您應該考慮一個函式,a -> [a] -> Bool因為您正在搜索一個函式,該函式檢查串列中的字符。
所以答案是:
filter (\x -> elem x [1..3]) [1..10]
uj5u.com熱心網友回復:
作為備選:
containsVowels :: Foldable t => t Char -> Bool
containsVowels = any (\ch -> (ch ==) `any` vowels)
where vowels = "aeiou"
如果字串中的任何字符等于中的任何字符vowels,則答案是True。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/427260.html
