[Solved]Question about how `show` deal with non-English character

Recently I have started a web project to learn haskell.When deal with some non-English character,e.g Chinese character “中文”. I notice when I use function like show or print,it will only output its unicode value,instead of the character itself:

GHCi, version 9.2.8: https://www.haskell.org/ghc/  :? for help
ghci> putStrLn "中文"
中文
ghci> show "中文"
"\"\\20013\\25991\""
ghci> print txt
"\20013\25991"
ghci> putStrLn $ show "中文"
"\20013\25991"

(Above code also represent the same as the compiled Haskell program)

It seems like the function like show or print deal with Showable cannot deal with non-English character.Is this a bug or shortage of Haskell?If not,can someone give me any advice to deal with non-English character?

(this is a noob question,I’m so sorry :frowning:

2 Likes

This is expected behaviour. The idea with show (and similarly print) is that it should be possible to copy its output into a Haskell source file to produce the same value. Thus, it prints these characters using escape codes to make this possible across platforms.

On the other hand, if you want to actually write a string to the terminal, you should use putStrLn, which is intended for precisely this purpose. (print is most useful for debugging, I feel.) On Windows it can sometimes result in garbled output, but that’s the fault of Windows, not Haskell!

1 Like

There was a proposal about changing that: Proposal: `showLitChar` (and `show @Char`) shouldn't escape readable Unicode characters · Issue #26 · haskell/core-libraries-committee · GitHub

But it was abandoned. There are also some workarounds in the thread you can use in ghci to get different behavior.

4 Likes

Thanks for your reply!I have read all the thread of this proposal and get some inspiration.

I will try my thoughts tonight.

1 Like