unicode(UTF-8)char上的Parsec输出

Fre*_*son 1 unicode haskell parsec utf-8 char

只需要了解与Parsec相关的内容.

parseTest (many1 alphaNum) "re2re1?"
"re2re1\916"
:t parseTest (many1 alphaNum) 
parseTest (many1 alphaNum) :: Text.Parsec.Prim.Stream s Data.Functor.Identity.Identity Char =>
 s -> IO ()
Run Code Online (Sandbox Code Playgroud)

因此,Unicode的输出(应该是UTF-8,因为我在OSX上)打印为十六进制(?)代码(应该是希腊三角形字符).现在,putChar不会在同一个ghci会话(和同一个终端)内进行相同的转换

Text.Parsec.Char> putChar '?'
?
Run Code Online (Sandbox Code Playgroud)

怎么会?它们应该只是'Char'类型以某种方式......?

Sib*_*ibi 6

这里的原因与方式有关show并且putChar已经实施.

?> show "re2re1?"
"\"re2re1\\916\""
?> mapM_ putChar "re2re1?"
re2re1?
Run Code Online (Sandbox Code Playgroud)

从源代码中可以看到Show实例for Char的定义如下:

instance  Show Char  where
    showsPrec _ '\'' = showString "'\\''"
    showsPrec _ c    = showChar '\'' . showLitChar c . showChar '\''

    showList cs = showChar '"' . showl cs
                 where showl ""       s = showChar '"' s
                       showl ('"':xs) s = showString "\\\"" (showl xs s)
                       showl (x:xs)   s = showLitChar x (showl xs s)
Run Code Online (Sandbox Code Playgroud)

putChar 像这样实现:

putChar         :: Char -> IO ()
putChar c       =  hPutChar stdout c
Run Code Online (Sandbox Code Playgroud)

parseTest函数在内部使用print本身内部使用的函数,这就是show为什么要获得delta的Unicode代码点值的原因.