erlang - 列表中的整数打印为 "\v"?

标签 erlang list-comprehension

谁能解释一下 Erlang 中这种意想不到的列表理解奇怪现象?

4> [A+B || [A, B] <- [1, 2, 3, [5, 6]]].
"\v"

预期:11

得到:"\v"

必须做什么才能获得预期的输出?

最佳答案

根据Erlang documentation :

A string is a list of codepoints, binaries with UTF-8-encoded codepoints (UTF-8 binaries), or a mix of the two.

"abcd"               % is a valid string  
<<"abcd">>           % is a valid string  
["abcd"]             % is a valid string  
<<"abc..åäö"/utf8>>  % is a valid string  
<<"abc..åäö">>       % is NOT a valid string,  
                     % but a binary with Latin-1-encoded codepoints  
[<<"abc">>, "..åäö"] % is a valid string  
[atom]               % is NOT a valid string

所以在上面你在列表或 [11] 中得到了整数 11:

Eshell V8.3  (abort with ^G)
1> [A+B || [A, B] <- [1, 2, 3, [5, 6]]].
"\v"
%% With hd/1 function you can get first element (head) of a list
2> hd([A+B || [A, B] <- [1, 2, 3, [5, 6]]]).
11
3> [11].
"\v"

Erlang VM 以字符串形式打印可打印列表。目前它支持两个可打印范围。 latin1 是默认值,unicode

$ erl
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V8.3  (abort with ^G)
1> io:printable_range().
latin1

您可以使用 +pclatin1 更改为 unicode标志:

$ erl
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V8.3  (abort with ^G)
1> [1662]. %% not printable in latin1 range
[1662]
2> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
$ erl +pc unicode
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.3  (abort with ^G)
1> [1662].
"پ"
2>

关于erlang - 列表中的整数打印为 "\v"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49204765/

相关文章:

python - 如果项目包含来自 "blacklist"的子字符串,则从列表中删除该项目

module - 我可以获得当前加载的所有模块的列表吗?

erlang - 有没有办法阻止 Erlang 服务器自动启动 epmd?

c++ - 是否有任何不是用 Java 编写的 Serious Graph 数据库?

Python 使用理解合并字典键和值

racket - for/list 是否做了不必要的反向操作?

erlang - 是否可以在 Erlang 中实现 Pregel 而无需 super 步骤?

Erlang文件追加模式

python - 替换列表/数据框中的项目的更好方法

Python - 还有其他方法可以在列表理解中应用函数和过滤器吗?