我正在查看枚举文档,这是我的代码:
defmodule Math do
def reverse(list), do: Enum.reverse(list)
end
Run Code Online (Sandbox Code Playgroud)
我运行它:
IO.write(Math.reverse([1,2,3,4,5,6,7,8]))
但是,一些奇怪的事情正在发生.我收到一声"嘟嘟"的声音,还有 这些有趣的人物 ......
我对Elixir很新,但我不知道从哪里开始调试过程.我哪里出错了?谢谢!
此问题与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
Run Code Online (Sandbox Code Playgroud)
这里发生的IO.write/1
是接收整数列表,并将其视为"char列表".使用单引号时可以创建字符列表,例如'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
Run Code Online (Sandbox Code Playgroud)
我猜这些角色被窗户奇怪地解释,这将导致声音效果和"笑脸".
编辑:入门文档也非常有用:http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#utf-8-and-unicode