How do I replace a character with multiple characters in a string. I can replace a character with another character with below:
replaceO [] = []
replaceO (x:xs) =
if x == '('
then '\n' : replaceO xs
else x : replaceO xs
But I want to replace '(' with "\n\t\t", not just '\n'.
---------- UPDATE -------------- Based on the answer below, can I make that a function that accepts a string and returns a string like this:
ReplaceFun str :: String -> String
ReplaceFun str = do
concatMap (\str -> if str == '.' then "foo" else [str])
This doesn't work, can someone point out my mistake? I am very new to Haskell.
Here is what I have for nested indentation:
replaceO (x:xs) n l =
if x == '('
then "\n" ++ (showTabs n "-") ++ replaceO xs (n + 1) 'l'
else
if x == ')'
then "\n" ++ (showTabs n "-") ++ replaceO xs (n - 1) 'l'
else x : replaceO xs n 'l'
concatMapenough arguments - the rhs ofreplaceFun(also note the lowercase) should have typeStringbutconcatMap (\str -> if str == '.' then "foo" else [str])has typeString -> String. You probably wantreplaceFun = concatMap (\str -> if str == '.' then "foo" else [str])