Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6362003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:51:03+00:00 2026-05-24T23:51:03+00:00

Haskell has a number of string literals that use the \ escape sequence. Ones

  • 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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:51:04+00:00Added an answer on May 24, 2026 at 11:51 pm

    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:    
    >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compile a haskell file that has a HUGE number of errors
since Haskell has such expressive type system, is there something supported directly that we
I'm doing some homework and while I have some experience with SML, Haskell has
It seems that Haskell has established several naming conventions around monads. Examples: appending T
Ive come across some answers (here in SO) saying that Haskell has many dark
Is it possible to implement a quicksort in Haskell (with RANDOM-PIVOT) that still has
I have a Haskell program that uses Gtk/GtkGLExt and runs fine on Linux (up-to-date
The Learn You a Haskell tutorial has an example of using a let binder
Let's say charm.c has an enum key and a function get_key() that returns a
This has to be a common question that all programmers have from time to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.