有人可以从Erlang解释这种意外的列表理解陌生感吗?
4> [A+B || [A, B] <- [1, 2, 3, [5, 6]]].
"\v"
预期:11
得到:"\v"
要获得预期的输出必须做些什么?
答案 0 :(得分:4)
字符串是代码点,具有UTF-8编码的代码点(UTF-8二进制文件)的二进制文件的列表,或两者的混合。
"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
您可以使用+pc标记将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> [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>