我必须制作三个函数来替换扁平字符串和列表.
我不知道,是否有像其他语言一样的替换功能.我搜索了但不幸的是没有成功:-(
所以我的尝试还很薄.
第一功能:
replace :: String -> String -> String -> String
replace findStr replaceStr myText = replace()??
Run Code Online (Sandbox Code Playgroud)
我的第一个功能的方法:
replace :: String -> String -> String -> String
replace [] old new = []
replace str old new = loop str
where
loop [] = []
loop str =
let (prefix, rest) = splitAt n str
in
if old == prefix -- found an occurrence?
then new ++ loop rest -- yes: replace
else head str : loop (tail …Run Code Online (Sandbox Code Playgroud)