I can't get GHCi or GHC to print unicode codepoint 221A (sqrt symbol: √).
I don't think it's my shell, because I can get ruby to do it:
irb> puts "\u221A"
√
GHC/GHCi is another issue:
ghci> putStrLn "\8730"
ghci> withFile "temp.out" WriteMode $ flip hPutStrLn "\8730"
ghci> readFile "temp.out"
"\SUB\n"
So 开发者_C百科what am I doing wrong?
(GHC v6.l0.3)
GHC's behavior with unicode changed in GHC 6.12.1 to "do the right thing" with Unicode strings. Prior versions truncate to 8 bit characters on IO (forcing the use of an encoding library).
That is, '\8730' is 0x221a, while '\SUB' is 0x1a -- the high byte is gone.
Here with GHC 7:
Prelude> print "√\n"
"\8730\n"
Prelude> putStr "√\n"
√
Prelude> putStr "\8730√\n"
√√
But I get your result with GHC 6.8. Like this:
Prelude> writeFile "/tmp/x" "√\n"
Prelude> readFile "/tmp/x"
"\SUB\n"
as the unicode bits are being truncated to 8 bits.
GHC 7 + IO works as expected:
Prelude> writeFile "/tmp/x" "\8730√\n"
Prelude> readFile "/tmp/x"
"\8730\8730\n"
Prelude> s <- readFile "/tmp/x"
Prelude> putStr s
√√
Can you upgrade to GHC 7 (in the Haskell Platform) to get full Unicode support? If this is not possible, you can use one of the encoding libraries, such as utf8-string
精彩评论