2016-05-12 30 views
2

我期待在enum文档,这里是我的代码:药剂返回笑脸上Enum.reverse方法

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相当陌生,但我不确定从何处开始调试过程。我哪里错了?谢谢!

+0

不是为了你的利益@MakeWebSocketsGreatAgain但对于其他人的利益谁可能会发现这样一个问题:https://github.com/elixir-lang/elixir/wiki/FAQ(项目4)。 –

回答

4

此问题与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正在接收整数的列表,并把它作为“字符列表”。使用单引号时可以创建char列表,​​如'foo'。使用给了我们很多的细节:

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

+1

嗯我现在看到了,谢谢你的深入解释! – MakeWebSocketsGreatAgain