Elixir在Enum.reverse方法上回归笑脸

时间:2016-05-12 23:26:41

标签: elixir

我正在查看enum文档,这是我的代码:

defmodule Math do
  def reverse(list), do: Enum.reverse(list)
end

我跑了它:

IO.write(Math.reverse([1,2,3,4,5,6,7,8]))

但是,有些奇怪的事情正在发生。我收到一声"哔哔"声音,伴随着  these funny characters ..

我对Elixir很新,但我不知道从哪里开始调试过程。我哪里出错了?谢谢!

1 个答案:

答案 0 :(得分:5)

此问题与Enum.reverse/1功能没有直接关系。只需将整数列表传递给IO.write/1

,就可以重现完全相同的事情
iex(5)> IO.write([8,7,6,5,4,3,2,1])
^H^G^F^E^D^C^B^A:ok

这里发生的是IO.write/1正在接收整数列表,并将其视为“字符列表”。使用单引号时可以创建字符列表,例如'foo'。使用i/1为我们提供了很多细节:

iex(22)> i('foo')
Term
  'foo'
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
  "char lists" (more precisely, a char list is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [102, 111, 111]
Reference modules
  List

我猜这些角色被窗户奇怪地解释,会导致声音效果和“笑脸”。

编辑:入门文档也很有用:http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#utf-8-and-unicode