Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell unicode(UTF-8)字符上的Parsec输出_Haskell_Unicode_Utf 8_Char_Parsec - Fatal编程技术网

Haskell unicode(UTF-8)字符上的Parsec输出

Haskell unicode(UTF-8)字符上的Parsec输出,haskell,unicode,utf-8,char,parsec,Haskell,Unicode,Utf 8,Char,Parsec,只需要理解一些与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 () 所以,Unicode的输出应该是UTF-8,因为我在OSX上,十六进制

只需要理解一些与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 ()
所以,Unicode的输出应该是UTF-8,因为我在OSX上,十六进制代码应该是希腊的delta字符。 现在,putChar在同一ghci会话和同一终端内不进行相同的转换

Text.Parsec.Char> putChar 'Δ'
Δ

为什么?它们都应该只是“Char”类型…?

这里的原因与show和putChar的实现方式有关

λ> show "re2re1Δ"
"\"re2re1\\916\""
λ> mapM_ putChar "re2re1Δ"
re2re1Δ
从源代码中,您可以看到Char的Show实例定义如下:

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)
putChar         :: Char -> IO ()
putChar c       =  hPutChar stdout c
putChar的实现方式如下:

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)
putChar         :: Char -> IO ()
putChar c       =  hPutChar stdout c

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

这里的原因与show和putChar的实现方式有关

λ> show "re2re1Δ"
"\"re2re1\\916\""
λ> mapM_ putChar "re2re1Δ"
re2re1Δ
从源代码中,您可以看到Char的Show实例定义如下:

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)
putChar         :: Char -> IO ()
putChar c       =  hPutChar stdout c
putChar的实现方式如下:

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)
putChar         :: Char -> IO ()
putChar c       =  hPutChar stdout c
parseTest函数在内部使用print函数,print函数本身在内部使用show,这就是为什么要获取delta的Unicode代码点值