Hi I am trying to remove the substring "\r"
by replacing it with an empty string in the following function:
clean :: String -> String
clean ('\\':'r':xs) = clean xs
clean (x:xs) = x : clean xs
clean "" = ""
But when I run it on a test input:
main = do
print (clean "test line\r\nnew test")
it doesn't work and just outputs the same string as the input string. Now the weird thing is that if I replace the backslashes with any other character, for example ':'
it works just fine:
clean :: String -> String
clean (':':'r':xs) = clean xs
clean (x:xs) = x : clean xs
clean "" = ""
main = do
print (clean "test line:r\nnew test")
Which outputs "test line\nnew test"
as intended. I suspect that I'm not escaping the double backslash correctly. For example the code:
main = do
print '\\'
outputs '\\'
instead of '\'
, but from what I've read this should be the proper way to escape it. I just can't figure out what I'm doing wrong, what am I missing?
\r
is one character. It works when you handle it that way:
clean :: String -> String
clean ('\r':xs) = clean xs
clean (x:xs) = x : clean xs
clean "" = ""
The backslash only needs to be escaped when you want to put an actual \
into the String literal.