I need to make three functions to replace flat lines and lists.
I do not know if there is a replacement function, as in other languages. I searched for this, but unfortunately without success: - (
So, my attempt is still very subtle.
1st function:
replace :: String -> String -> String -> String replace findStr replaceStr myText = replace()??
My approach to the first 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
Second function:
replaceBasedIdx :: String -> [String] -> String -> String replaceBasedIdx findStr replaceStrList myText = replace()???
This function should replace the first findStr in myTxt with the first replaceStrList element, the second findStr with the second element, and so on ...
Example:
replaceBasedIdx "a" ["G","V","X"] "Haskell is a language" "HGskell is V lXnguage"
My approach for the second 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 am now very close to the final result, but the counter does not increase.
Could you tell me where is my mistake? And how could I modify the 1st or 2nd function to get the third function?
3rd function:
replaceBasedIdxMultiple :: [String] -> [String] -> String -> String replaceBasedIdxMultiple findStrList replaceStrList myText = replace()???
This function should replace each findStrList element in myTxt with the corresponding element from replaceStrList, therefore 1. with 1., 2. with 2. and so on.
Example:
replaceBasedIdxMultiple ["A","X","G"] ["N","Y","K"] "ABXMG" "NBYMK"
could you help me? some tips and tricks how to start from this?
I'm really scattered: - (
Thank you very much in advance
Good hello!