Alex Rivera | Logout

Haskell: Suppress quotes around strings when shown

Asked 2012-08-24T03:46:30.540
31

The following code:

data HelloWorld = HelloWorld; 
instance Show HelloWorld where show _ = "hello world";

hello_world = "hello world"

main = putStr $ show $ (HelloWorld, hello_world)

Prints:

(hello world,"hello world")

I'd like it to print:

(hello world,hello world)

i.e. I want behaviour like the following:

f "hello world" = "hello world"
f HelloWorld = "hello world"

Unfortunately, show does not satisfy this, as:

show "hello world" = "\"hello world\""

Is there a function that works like f that I've described above?

Edit
Report

1 Answer

24

Firstly, take a look at this question. Maybe you will be satisfied with toString function.

Secondly, show is a function that maps some value to a String.

So, it makes sense that quotes should be escaped:

> show "string"
"\"string\""

Is there a function that works like f that I've described above?

Seems like you're looking for id:

> putStrLn $ id "string"
string
> putStrLn $ show "string"
"string"
answered 2012-08-24T07:01:27.887

Your Answer