Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: How to get "\\0" into "\0"?

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 
like image 807
Snoqual Avatar asked Aug 24 '11 06:08

Snoqual


1 Answers

You don't need to do anything. When you input the string literal

let s = "Newline: \\n Tab: \\t"

you can check that it is what you want:

Prelude> putStrLn s
Newline: \n Tab: \t
Prelude> length s
19

If you just ask ghci for the value of s you'll get something else,

Prelude> s
"Newline: \\n Tab: \\t"

apparently it's doing some escape formatting behind your back, and it also displays the quotes. If you call show or print you'll get yet other answers:

Prelude> show s
"\"Newline: \\\\n Tab: \\\\t\""
Prelude> print s
"Newline: \\n Tab: \\t"

This is because show is meant for serializing values, so when you show a string you don't get the original back, you instead get a serialized string which can be parsed into the original string. The result of show s is actually displayed by print s (print is defined as putStrLn . show). When you just show s in ghci you get an even stranger answer; here ghci is formatting the characters which are serialized by show.

tl;dr - always use putStrLn to see what the value of a string is in ghci.

Edit: I just realized that maybe you want to convert the literal value

Newline: \n Tab: \t

into the actual control sequences. The easiest way to do this is probably to stick it in quotes and use read:

Prelude> let s' = '"' : s ++ "\""
Prelude> read s' :: String
"Newline: \n Tab: \t"
Prelude> putStrLn (read s')
Newline: 
 Tab:   

Edit 2: an example of using readLitChar, this is very close to Chris's answer except with readLitChar:

strParser :: ReadP String
strParser = do
  str <- many (readS_to_P readLitChar)
  eof
  return str

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.)

> putStrLn . fst . head $ readP_to_S strParser s
Newline:
Tab:    
>
like image 121
John L Avatar answered Oct 10 '22 15:10

John L