Haskell has a number of string literals that use the \ escape sequence. Ones such as \n, \t, \NUL.
If I have the string literal:
let s = "Newline: \\n Tab: \\t"
how do I define the function escape :: String -> String that will convert the above string to:
"Newline: \n Tab: \t"
And the same with all other string literal escape sequences.
I’m okay with using Quasi Quoting and Template Haskell, but don’t know how to use them to achieve the result. Any pointers?
Update: I just found the Text.ParserCombinators.ReadP module that’s included in the Base library. It supports the readLitChar :: ReadS Char function in Data.Char that does what I want, but I don’t know how to use the ReadP module. I tried the following and it works:
escape2 [] = []
escape2 xs = case readLitChar xs of
[] -> []
[(a, b)] -> a : escape2 b
But this may not be the right way to use the ReadP module. Can anyone provide some pointers?
Another update: Thanks everyone. My final function below. Not bad, I think.
import Text.ParserCombinators.ReadP
import Text.Read.Lex
escape xs
| [] <- r = []
| [(a,_)] <- r = a
where r = readP_to_S (manyTill lexChar eof) xs
You don’t need to do anything. When you input the string literal
you can check that it is what you want:
If you just ask ghci for the value of
syou’ll get something else,apparently it’s doing some escape formatting behind your back, and it also displays the quotes. If you call
showorprintyou’ll get yet other answers:This is because
showis meant for serializing values, so when youshowa string you don’t get the original back, you instead get a serialized string which can be parsed into the original string. The result ofshow sis actually displayed byprint s(printis defined asputStrLn . show). When you justshow sin ghci you get an even stranger answer; here ghci is formatting the characters which are serialized byshow.tl;dr – always use
putStrLnto see what the value of a string is in ghci.Edit: I just realized that maybe you want to convert the literal value
into the actual control sequences. The easiest way to do this is probably to stick it in quotes and use
read:Edit 2: an example of using
readLitChar, this is very close to Chris’s answer except withreadLitChar:Then you run it with
readP_to_S, which gives you a list of matching parses (there shouldn’t be more than one match, however there might not be any match so you should check for an empty list.)