带有非英文字符的 Haskell IO

发布于 2024-09-17 06:27:46 字数 285 浏览 6 评论 0原文

看看这个,我正在尝试

appendFile "out" $ show 'д'

“д”是俄语字母表中的字符。 之后“out”文件包含:

'\1076'

我的理解是字符“д”的unicode数字代码。为什么会出现这样的情况呢?我怎样才能得到我的角色的正常表现?

有关更多信息,效果很好:

appendFile "out"  "д"

谢谢。

Look at this , i am try

appendFile "out" $ show 'д'

'д' is character from Russian alphabet.
After that "out" file contains:

'\1076'

How i understand is the unicode numeric code of character 'д'. Why is it happens ? And How i can to get the normal representation of my character ?

For additional information it is works good:

appendFile "out"  "д"

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

前事休说 2024-09-24 06:27:46

show 对 ASCII 范围之外的所有字符(以及 ASCII 范围内的一些字符)进行转义,因此不要使用 show

因为“д”工作正常,所以就使用它。如果你不能,因为 д 实际上在变量内部,你可以使用 [c] (其中 c 是包含该字符的变量。如果你需要用单引号将它引起来(就像 show 那样) ),您可以使用['\'', c, '\'']

show escapes all characters outside the ASCII range (and some inside the ASCII range), so don't use show.

Since "д" works fine, just use that. If you can't because the д is actually inside a variable, you can use [c] (where c is the variable containing the character. If you need to surround it by single quotes (like show does), you can use ['\'', c, '\''].

一个人的夜不怕黑 2024-09-24 06:27:46

阅读您对我的评论的回复后,我认为您的情况是您有一些数据结构,可能是 [(String,String)] 类型,并且您希望输出它以用于调试目的。使用 show 会很方便,但它会转义非 ASCII 字符。

这里的问题不在于 unicode,您需要一个能够正确格式化数据以供显示的函数。我认为 show 不是正确的选择,部分原因是转义某些字符存在问题。您需要的是像 Show 这样的类型类,但它显示数据以供读取而不是转义字符。也就是说,您需要一个漂亮的打印机,它是一个提供格式化数据以供显示的函数的库。 Hackage 上有几种漂亮的打印机,我会看看 uulibwl-pprint 启动。我认为两者都适合,无需太多工作。

这是 uulib 工具的示例。使用 Pretty 类型类代替 Show,该库附带了许多有用的实例。

import UU.PPrint

-- | Write each item to StdOut
logger :: Pretty a => a -> IO ()
logger x = putDoc $ pretty x <+> line

在 ghci 中运行:

Prelude UU.PPrint> logger 'Д'
Д 
Prelude UU.PPrint> logger ('Д', "other text", 54)
(Д,other text,54) 
Prelude UU.PPrint> 

如果您想输出到文件而不是控制台,可以使用 hPutDoc 函数输出到句柄。您还可以调用 renderSimple 来生成 SimpleDoc,然后在构造函数上进行模式匹配来处理输出,但这可能会更麻烦。无论你做什么,都要避免 show

Prelude UU.PPrint> show $ pretty 'Д'
"\1044"

你也可以编写自己的类似于 show 的类型类,但按照你喜欢的格式进行格式化。如果您选择这条路线,Text.Printf 模块会很有帮助。

After reading your reply to my comment, I think your situation is that you have some data structure, maybe with type [(String,String)], and you'd like to output it for debugging purposes. Using show would be convienent, but it escapes non-ASCII characters.

The problem here isn't with the unicode, you need a function that will properly format your data for display. I don't think show is the right choice, in part because of the problems with escaping some characters. What you need is a type class like Show, but one that displays data for reading instead of escaping characters. That is, you need a pretty-printer, which is a library that provides functions to format data for display. There are several pretty-printers available on Hackage, I'd look at uulib or wl-pprint to start. I think either would be suitable without too much work.

Here's an example with the uulib tools. The Pretty type class is used instead of Show, the library comes with many useful instances.

import UU.PPrint

-- | Write each item to StdOut
logger :: Pretty a => a -> IO ()
logger x = putDoc $ pretty x <+> line

running this in ghci:

Prelude UU.PPrint> logger 'Д'
Д 
Prelude UU.PPrint> logger ('Д', "other text", 54)
(Д,other text,54) 
Prelude UU.PPrint> 

If you want to output to a file instead of the console, you can use the hPutDoc function to output to a handle. You could also call renderSimple to produce a SimpleDoc, then pattern match on the constructors to process output, but that's probably more trouble. Whatever you do, avoid show:

Prelude UU.PPrint> show $ pretty 'Д'
"\1044"

You could also write your own type class similar to show but formatted as you like it. The Text.Printf module can be helpful if you go this route.

第几種人 2024-09-24 06:27:46

使用数据.文本。它为 IO 提供区域设置感知和编码支持。

Use Data.Text. It provides IO with locale-awareness and encoding support.

世态炎凉 2024-09-24 06:27:46

在网络上快速搜索“UTF Haskell”应该会为您提供良好的链接。最推荐的包可能是 text 包。

import Data.Text.IO as UTF
import Data.Text as T

main = UTF.appendFile "out"  (T.pack "д")

A quick web search for "UTF Haskell" should give you good links. Probably the most recommended package is the text package.

import Data.Text.IO as UTF
import Data.Text as T

main = UTF.appendFile "out"  (T.pack "д")
人生戏 2024-09-24 06:27:46

要按节目显示国家字符,请输入您的代码:

{-# LANGUAGE FlexibleInstances #-}

instance {-# OVERLAPPING #-} Show String where
    show = id

您可以尝试:

*Main> show "ł"
ł
*Main> show "ą"
ą
*Main> show "ę"
ę
*Main> show ['ę']
ę
*Main> show ["chleb", "masło"]
[chleb,masło]
*Main> data T = T String deriving (Show)
*Main> t = T "Chleb z masłem"
*Main> t
T Chleb z masłem
*Main> show t
T Chleb z masłem

To display national characters by show, put in your code:

{-# LANGUAGE FlexibleInstances #-}

instance {-# OVERLAPPING #-} Show String where
    show = id

You can try then:

*Main> show "ł"
ł
*Main> show "ą"
ą
*Main> show "ę"
ę
*Main> show ['ę']
ę
*Main> show ["chleb", "masło"]
[chleb,masło]
*Main> data T = T String deriving (Show)
*Main> t = T "Chleb z masłem"
*Main> t
T Chleb z masłem
*Main> show t
T Chleb z masłem
来世叙缘 2024-09-24 06:27:46

我之前的解决方案中没有引号。另外,我现在把代码放在模块中,模块必须导入到你的程序中。

{-# LANGUAGE FlexibleInstances #-}

module M where

instance {-# OVERLAPPING #-} Show String where
    show x = ['"'] ++ x ++ ['"']

给初学者的信息:请记住,该节目不显示任何内容。 show 将数据转换为带有附加格式字符的字符串。

我们可以在WinGHCi中尝试:
通过 WinGHCi 自动

*M> "ł"
"ł"
*M> "ą"
"ą"
*M> "ę"
"ę"
*M> ['ę']
"ę"
*M> ["chleb", "masło"]
["chleb","masło"]
*M> data T = T String deriving (Show)
*M> t = T "Chleb z masłem"

或手动

*M> (putStrLn . show) "ł"
"ł"
*M> (putStrLn . show) "ą"
"ą"
*M> (putStrLn . show) "ę"
"ę"
*M> (putStrLn . show) ['ę']
"ę"
*M> (putStrLn . show) ["chleb", "masło"]
["chleb","masło"]
*M> data T = T String deriving (Show)
*M> t = T "Chleb z masłem"
*M> (putStrLn . show) t
T "Chleb z masłem"

在要显示的代码中:

putStrLn "ł"
putStrLn "ą"
putStrLn "ę"
putStrLn "masło"
(putStrLn . show) ['ę']
(putStrLn . show) ["chleb", "masło"]
data T = T String deriving (Show)
t = T "Chleb z masłem"
(putStrLn . show) t

我正在为 Google 添加标签“polskie znaki haskell”。

There were no quotes in my previous solution. In addition, I put the code in the module now and the module must be imported into your program.

{-# LANGUAGE FlexibleInstances #-}

module M where

instance {-# OVERLAPPING #-} Show String where
    show x = ['"'] ++ x ++ ['"']

Information for beginners: remember that the show does not display anything. show converts data to string with additional formatting characters.

We can try in WinGHCi:
automaticaly by WinGHCi

*M> "ł"
"ł"
*M> "ą"
"ą"
*M> "ę"
"ę"
*M> ['ę']
"ę"
*M> ["chleb", "masło"]
["chleb","masło"]
*M> data T = T String deriving (Show)
*M> t = T "Chleb z masłem"

or manualy

*M> (putStrLn . show) "ł"
"ł"
*M> (putStrLn . show) "ą"
"ą"
*M> (putStrLn . show) "ę"
"ę"
*M> (putStrLn . show) ['ę']
"ę"
*M> (putStrLn . show) ["chleb", "masło"]
["chleb","masło"]
*M> data T = T String deriving (Show)
*M> t = T "Chleb z masłem"
*M> (putStrLn . show) t
T "Chleb z masłem"

In code to display:

putStrLn "ł"
putStrLn "ą"
putStrLn "ę"
putStrLn "masło"
(putStrLn . show) ['ę']
(putStrLn . show) ["chleb", "masło"]
data T = T String deriving (Show)
t = T "Chleb z masłem"
(putStrLn . show) t

I'm adding tag "polskie znaki haskell" for Google.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文