Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何使用isSuffixOf测试两个后缀_Haskell - Fatal编程技术网

Haskell 如何使用isSuffixOf测试两个后缀

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

所以我有一个字符串列表,比如[“test1”、“test2”、“test3”、“exam1”],我想知道哪些字符串以“1”或“2”结尾。 我知道我可以用

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"]