我必须做出三个功能为平串,并在列表中替换。
我不知道,是否有像在其他语言中替换功能。 我搜索的结果不过可惜没有成功:-(
所以我尝试又是相当薄。
第一个功能:
replace :: String -> String -> String -> String
replace findStr replaceStr myText = replace()??
我的第一功能的方法:
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 str) -- no: keep looking
n = length old
第2个功能:
replaceBasedIdx :: String -> [String] -> String -> String
replaceBasedIdx findStr replaceStrList myText = replace()???
这个功能应该与replaceStrList,第二FINDSTR与第二个元素等的第1个要素代替第一FINDSTR在myTxt ...
例:
replaceBasedIdx "a" ["G","V","X"] "Haskell is a language"
"HGskell is V lXnguage"
我的第二个功能的方法:
replaceBasedIdx :: String -> [String] -> String -> String
replaceBasedIdx findStr replaceStrList myText = replaceBasedIdxSub findStr replaceStrList myText 0
replaceBasedIdxSub :: String -> [String] -> String -> Int -> String
replaceBasedIdxSub findStr replaceStrList myText counter = loop myText
where
loop [] = []
loop myText =
let (prefix, rest) = splitAt n myText
in
if findStr == prefix -- found an occurrence?
then (replaceStrList !! (counter+1)) ++ loop rest -- yes: replace it
else head myText : loop (tail myText) -- no: keep looking
n = length findStr
我现在非常接近最终结果,但是计数器不递增。
你能告诉我,我的错误是什么? 我怎么能modifey第一或第二个函数来取得第三个功能也?
第三个功能:
replaceBasedIdxMultiple :: [String] -> [String] -> String -> String
replaceBasedIdxMultiple findStrList replaceStrList myText = replace()???
此函数应与来自replaceStrList相应元素替换findStrList中的每个元素在myTxt,所以1. 1.,2与2等...
例:
replaceBasedIdxMultiple ["A","X","G"] ["N","Y","K"] "ABXMG"
"NBYMK"
你能帮助我吗? 一些技巧和提示,如何与它开始?
我真的很不同:-(
非常感谢提前
样的问候!