gpt4 book ai didi

elixir - List.flatten 返回意外值

转载 作者:行者123 更新时间:2023-12-02 00:52:40 27 4
gpt4 key购买 nike

版本:1.2.2,错误:

List.flatten ([a,[b]]) is expected to return ([a,b]). However, this does not work properly for some cases. For example, List.flatten ([11, [[12], 13]]) returns '\v\f\r', when ([11,12,13]) is expected. Even List.flatten([10]) returns '\n'.

为什么会发生这种情况,解决方法是什么(如果有)?

最佳答案

如果您的列表由整数组成,这些整数都可以表示 ASCII 集中的可打印 UTF-8 代码点,它将作为字符列表输出到终端。

iex> [104,101,108,108,111]
'hello'

但它仍然是一个列表:

iex> 'hello' ++ ' there'
'hello there'

如果它包含任何不可打印的代码点,它将作为标准列表输出:

iex> 'hello' ++ [0]
[104, 101, 108, 108, 111, 0]

您可以使用 ? 运算符查看字符的代码点:

iex> ?h
104

我们可以使用 iex 中的 i 助手获取有关该术语的信息:

iex> i 'hello'
Term
'hello'
Data type
List
Description
This is a list of integers that is printed as a sequence of characters
delimited by single quotes because all the integers in it represent valid
ASCII characters. Conventionally, such lists of integers are referred to
as "charlists" (more precisely, a charlist is a list of Unicode codepoints,
and ASCII is a subset of Unicode).
Raw representation
[104, 101, 108, 108, 111]
Reference modules
List

为什么 elixer 会这样做?二郎。

关于elixir - List.flatten 返回意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38458166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com