[英]Why does my iex return a '-C' or a '-A' when I run this function
I've been learning Elixir for awhile now but I came across something today that totally confused me. 我已经学习Elixir已有一段时间了,但是今天我遇到了一些使我完全困惑的事情。
I made this filtering function: 我做了这个过滤功能:
thingy = for a <- ["may", "lay", "45", "67", "bay", "34"], do: Integer.parse(a)
for {n, _} <- thingy, do: n
output: '-C"'
Completely unexpected output, yet the version below 'works' 完全出乎意料的输出,但“工作”下面的版本
parseds = for i <- [ "10", "hot dogs", "20" ], do: Integer.parse(i)
for {n, _} <- parseds, do: n
output: [10, 20]
However if I change the numbers to something like 45 and 65 I get '-A'
as the result. 但是,如果我将数字更改为45和65
'-A'
类的结果,则会得到'-A'
。
Is this just the underlying binary functions permitting me from using which numbers I like? 这仅仅是允许我使用喜欢的数字的基本二进制函数吗?
This is because Elixir, like Erlang, doesn't internally have a String type . 这是因为Elixir和Erlang一样,内部没有String类型 。 Single-quoted strings are represented as character lists, and these are commonly used when dealing with Erlang libraries.
单引号字符串表示为字符列表,这些字符串通常在处理Erlang库时使用。 When you give Elixir the list
[45, 67, 34]
, it displays it as a list of ASCII characters 46, 67, and 34 ; 当您为Elixir提供列表
[45, 67, 34]
45、67、34 [45, 67, 34]
,它将显示为ASCII字符列表46、67和34 ; which are -
, C
, and "
. 分别是
-
, C
和"
。
If at least one number in your list doesn't represent a printable character, you see the list of numbers. 如果列表中至少有一个数字不代表可打印字符,则会看到数字列表。 Because
10
doesn't map to a printable character, in the second example, you see 10
and 20
. 因为
10
不会映射到可打印的字符,所以在第二个示例中,您看到10
和20
。
It's important to note that the list you've created is still internally represented as [45, 67, 34]
so any list operations you do will work exactly as you'd expect with your numbers. 重要的是要注意,您创建的列表在内部仍表示为
[45, 67, 34]
因此您执行的任何列表操作都将完全按照您期望的数字进行工作。
This is because Elixir, like Erlang, doesn't internally have a String type
这是因为Elixir与Erlang一样,内部没有String类型
Whatever that means. 不管什么意思。 Strings, Smings.
弦乐,杂物。 It's as simple as:
就像这样简单:
iex(4)> [45, 67, 34]
'-C"'
In iex, a list of numbers is interpreted as a sequence of characters, where each number is a numerical code for some character. 在iex中,数字列表被解释为字符序列,其中每个数字都是某个字符的数字代码。 If you look at an ascii chart , you will see that:
如果查看ASCII图表 ,您将看到:
45 -> -
67 -> C
34 -> "
Look at this: 看这个:
iex(5)> 'hi' == [104, 105]
true
In Elixir, [104, 105]
and [45, 67, 34]
are called charlists. 在Elixir中,
[104, 105]
和[45, 67, 34]
45,67,34 [45, 67, 34]
被称为字符列表。 A shortcut for creating the charlist [104, 105]
is 'hi'
. 创建字符列表
[104, 105]
的快捷方式是'hi'
。 This is the result of a terrible feature of Erlang, but because Elixir is able to interface with Erlang, there is a need for charlists. 这是Erlang 可怕功能的结果,但是由于Elixir能够与Erlang交互,因此需要字符列表。 An Elixir charlist is the equivalent of an Erlang string, and an Erlang string is equivalent to a list of numbers.
Elixir字符列表等效于Erlang字符串,而Erlang字符串等效于数字列表。
Suppose your Elixir program does a bunch of critical mathematical calculations with the result being: 假设您的Elixir程序进行了大量的关键数学计算,结果是:
result = [76, 64, 78, 79]
and you want to display that info to the user so that they can tune a defibrillator's settings in order to save a patient's life, so you do this: 并且您想向用户显示该信息,以便他们可以调整除颤器的设置以挽救患者的生命,因此您可以执行以下操作:
IO.puts "Set the defibrillator dials to these numbers: #{result}"
Here's what the user will see: 用户将看到以下内容:
Set the defibrillator dials to these numbers: L@NO
Patient dies here. 患者在这里死亡。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.