Haskell - Is there a replacement function?

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 -- found an occurrence? then new ++ loop rest -- yes: replace else head str : loop (tail str) -- no: keep looking n = length old 

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!

+4
source share
2 answers

replace exists in Data.List.Utils , part of the MissingH package.

Actually, this is a very concise implementation:

 replace :: Eq a => [a] -> [a] -> [a] -> [a] replace old new = join new . split old 
+6
source

First, join is a bad name which is already a standard function . Also, I have no idea why you define this function this way - it does not seem to do anything useful.

But well, you tried something. So, let's find the right solution now ...

As usual, a good idea in Haskell, we want to break this down into subtasks. First of all, you need to find the substrings that you would like to replace. It might look something like this:

 locateSub :: (Eq a) => [a] -- ^ The sought sublist. -> [a] -- ^ The source list. -> Maybe ([a],[a]) -- ^ Everything to the left and, if found, everything -- to the right of the sought sublist. No need to return -- the sublist itself in between since we already know it! 

Using this function, replace is straightforward:

 replace oldSub newSub list = case locateSub oldSub list of Nothing -> list -- Sublist not found: we're done already! Just (l, r) -> l ++ newSub ++ replace oldSub newSub r 

replaceBasedIdx not much more complicated, you only need to list the newSub list, and not always pass it as it is.

So you need to execute locateSub . With isPrefixOf you're on the right track. In fact, it is very similar to your _replace (By the way: in Haskell, it only uses ' , not underscores, to call the' local options / helpers 'of the function, so it's better to call it replace' .)

+1
source

Source: https://habr.com/ru/post/1479606/


All Articles