Haskell 如何使用isSuffixOf测试两个后缀
所以我有一个字符串列表,比如[“test1”、“test2”、“test3”、“exam1”],我想知道哪些字符串以“1”或“2”结尾。 我知道我可以用Haskell 如何使用isSuffixOf测试两个后缀,haskell,Haskell,所以我有一个字符串列表,比如[“test1”、“test2”、“test3”、“exam1”],我想知道哪些字符串以“1”或“2”结尾。 我知道我可以用 map (isSuffixOf "1") ["test1", "test2", "test3", "exam1"] map (isSuffixOf "2") ["test1", "test2", "test3", "exam1"] 要得到两个不同的布尔值列表,但有什么方法可以同时测试这两个布尔值吗?我试过这两种变体 map (isSuffix
map (isSuffixOf "1") ["test1", "test2", "test3", "exam1"]
map (isSuffixOf "2") ["test1", "test2", "test3", "exam1"]
要得到两个不同的布尔值列表,但有什么方法可以同时测试这两个布尔值吗?我试过这两种变体
map (isSuffixOf "1" || "2") ["test1", "test2", "test3", "exam1"]
map (isSuffixOf "1" || isSuffixOf "2") ["test1", "test2", "test3", "exam1"]
但是他们都给了我一个错误你可以在这里使用箭头
import Control.Arrow
f = isSuffixOf "1" &&& isSuffixOf "2"
result = map (uncurry (||)) . map f $ ["test1", "test2", "test3", "exam1"]
或者,更简单地说,写一个comp列表
result = [isSuffix "1" x || isSuffix "2" x | x <- ["test1", "test2", "test3", "exam1"]]
可以使用lambda表达式指定映射函数
map (\x -> isSuffixOf "1" x || isSuffixOf "2" x) ["test1", "test2", "test3", "exam1"]
或者,您可以使用(>)a的应用程序实例来构建函数
map ((||) <$> isSuffixOf "1" <*> isSuffixOf "2") ["test1", "test2", "test3", "exam1"]
map ((||) <$> isSuffixOf "1" <*> isSuffixOf "2") ["test1", "test2", "test3", "exam1"]
map (liftA2 (||) (isSuffixOf "1") (isSuffixOf "2")) ["test1", "test2", "test3", "exam1"]