Selectively replace multiple instances of a character within a string: Difference between revisions

Content added Content deleted
Line 153: Line 153:
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe)


---------- POSITIONAL CHARACTER REPLACEMENT RULES --------
---------- POSITIONAL CHARACTER-REPLACEMENT RULES --------


nthCharsReplaced :: M.Map Char [Maybe Char] -> String -> String
nthCharsReplaced :: M.Map Char [Maybe Char] -> String -> String
Line 159: Line 159:
where
where
go a c =
go a c =
let i = fromMaybe 0 (M.lookup c a)
if M.member c ruleMap
in ( M.insert c (succ i) a,
then
otherChar i c (fromMaybe [] (M.lookup c ruleMap))
let i = fromMaybe 0 (M.lookup c a)
)
in ( M.insert c (succ i) a,
otherChar i c (fromMaybe [] (M.lookup c ruleMap))
)
else (a, c)


otherChar :: Int -> Char -> [Maybe Char] -> Char
otherChar :: Int -> Char -> [Maybe Char] -> Char
Line 168: Line 171:
| i < length deltas = fromMaybe c (deltas !! i)
| i < length deltas = fromMaybe c (deltas !! i)
| otherwise = c
| otherwise = c



--------------------------- TEST -------------------------
--------------------------- TEST -------------------------