Haskell 用相应的替换替换多个子字符串
我有一个Haskell 用相应的替换替换多个子字符串,haskell,replace,Haskell,Replace,我有一个字符串,它包含几个标签,格式为${…}(其中..可以是任何不包含}字符的字符串),例如foo${bar}baz${qux} 我想替换这些标记,但要替换这些标记,我需要以下形式的函数: replace :: [String] -> [String] -> String -> String -- tags replacements target result replace ["${bar}", "${qux}"] ["abc", "def
字符串
,它包含几个标签,格式为${…}
(其中..
可以是任何不包含}
字符的字符串),例如foo${bar}baz${qux}
我想替换这些标记,但要替换这些标记,我需要以下形式的函数:
replace :: [String] -> [String] -> String -> String
-- tags replacements target result
replace ["${bar}", "${qux}"] ["abc", "def"] "foo ${bar} baz ${qux}" == "foo abc baz def"
(当将数组作为参数时,这类似于PHP的函数。)
我在任何包中都找不到这样的替换函数。有这样一个函数吗?如果没有,我将如何编写它(指向正确的方向就足够了;我正在学习Haskell)?作为一行程序:
Prelude Data.Text> Prelude.foldr (uncurry Data.Text.replace) "foo ${bar} baz ${qux}" $ Prelude.zip ["${bar}", "${qux}"] ["abc", "def"]
"foo abc baz def"
换言之:
replace as bs x = Prelude.foldr (uncurry Data.Text.replace) x $ Prelude.zip as bs