haskell:从控制台读取特殊字符

Ale*_*lex 6 unicode haskell

我想从控制台读取一个包含特殊字符的字符串,如ö,ä,ü,μ......我试过:

做... ts < - getLine ...

但这对那些角色不起作用.例如,ö的unicode是\ 246,但是如果我使用getLine读取öhaskell读取"\ 195\182",并且putStr"\ 195\182"给我,这不是ö.这有什么问题?我是否需要其他功能来读取这些字符?

我在Windows XP上使用WinGHCi 7.0.3.如果有人可以帮助我,我会很高兴因为到目前为止我找不到任何东西.


@Judah Jacobson:

在输入任何其他命令之前我再次尝试了,并得到了这个:

Prelude> :m +System.IO
Prelude System.IO> hSetEncoding stdin utf8
Prelude System.IO> getLine
????
"\206\177\207\402\206\180\207\8224"
Prelude System.IO> putStr "\206\177\207\402\206\180\207\8224"
ασδφPrelude System.IO> 
Run Code Online (Sandbox Code Playgroud)

我也试过了windows命令chcp 65001,但它没有改变任何东西,我已经在windows中激活了utf8.

fir*_*dle 2

您需要将 stdin 的编码设置为 UTF8。对我来说,在 Windows XP 上的 GHCi 中最初设置为 CP437,在 Mac 上设置为 UTF8。

\n\n

检查hGetEncoding stdin(System.IO),并设置hSetEncoding stdin utf8它应该可以工作。

\n\n

编辑:这就是我的 Mac 上的样子:

\n\n
Prelude System.IO> hSetEncoding stdin latin1\nPrelude System.IO> str <- getLine\n\xc3\xb6\nPrelude System.IO> putStr str\n\xc3\x83\xc2\xb6Prelude System.IO> print str\n"\\195\\182"\nPrelude System.IO> hSetEncoding stdin utf8\nPrelude System.IO> str <- getLine\n\xc3\xb6\nPrelude System.IO> putStr str\n\xc3\xb6Prelude System.IO> print str\n"\\246"\n
Run Code Online (Sandbox Code Playgroud)\n