I have to make three functions for replacing of flat strings and in lists.
I don't know, whether there is a replace function like in other languages. I searched for that however unfortunately without success :-(
So my attempt is yet quite thin.
1st function:
replace :: String -> String -> String -> String
replace findStr replaceStr myText = replace()??
My approach for the 1st function:
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
2nd function:
replaceBasedIdx :: String -> [String] -> String -> String
replaceBasedIdx findStr replaceStrList myText = replace()???
This function should replace the 1st findStr in myTxt with the 1st element of replaceStrList, the 2nd findStr with the 2nd element and so on...
Example:
replaceBasedIdx "a" ["G","V","X"] "Haskell is a language"
"HGskell is V lXnguage"
My approach for the 2nd function:
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
I'm now very near to the final result, however the counter doesn't increment.
Could you please tell me, where my mistake is? And how could I modifey the 1st or 2nd function to get the 3rd function also?
3rd function:
replaceBasedIdxMultiple :: [String] -> [String] -> String -> String
replaceBasedIdxMultiple findStrList replaceStrList myText = replace()???
This function should replace each element of findStrList in myTxt with the corresponding element from the replaceStrList, so 1. with 1., 2. with 2. and so on...
Example:
replaceBasedIdxMultiple ["A","X","G"] ["N","Y","K"] "ABXMG"
"NBYMK"
Could you help me with this? some tips and hints, how to begin with it?
I'm really disparate :-(
Thanks a lot in advance
Kind greetings!