elixir - List.flatten 返回意外值

标签 elixir

版本: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/

相关文章:

xml - 如何处理 sweet_xml 中的不区分大小写?

elixir - 使用Phoenix框架查看当前年份

elixir - 使用 :simple_one_for_one strategy 时的子参数

elixir - ExUnit 断言

elixir - 如何访问 Elixir 中的当前模块

elixir - 步骤列表与 Elixir

phoenix-framework - Phoenix + Addict - POST 操作的跨域错误

elixir - 如何映射 map 列表并添加它们?

scala - 从 rpc 调用到其他节点的错误?

erlang - 为什么我的 iex session 的 pid 总是相同?