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 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!