为什么x = x +1在Elixir中有效? [英] Why is x = x +1 valid in Elixir?

查看:91
本文介绍了为什么x = x +1在Elixir中有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Elixir的所有内容,我都认为应将分配视为模式匹配。如果是这样,那么为什么x = x + 1在Elixir中起作用?没有x的值x = x + 1。

Everything I've read about Elixir says that assignment should be thought of as pattern matching. If so then why does x = x + 1 work in Elixir? There is no value of x for which x = x + 1.

推荐答案


我已经完成的所有操作阅读有关Elixir的内容时,应将分配视为模式匹配。

Everything I've read about Elixir says that assignment should be thought of as pattern matching.

在Elixir中, = 称为模式匹配运算符,但其工作方式与Erlang中的模式匹配运算符不同。这是因为在Elixir中变量不是像Erlang中那样的单一赋值。这就是Erlang的工作方式:

In Elixir, = is called the pattern match operator, but it does not work the same way as the pattern match operator in Erlang. That's because in Elixir variables are not single assignment like they are in Erlang. Here's the way Erlang works:

~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3  (abort with ^G)

1> X = 15.
15

2> X = 100.
** exception error: no match of right hand side value 100

3> X.
15

4> 

因此,在Erlang中,此操作失败:

Therefore, in Erlang this fails:

4> X = X + 1.
** exception error: no match of right hand side value 16

使用Erlang的单个赋值,事情非常简单:因为X已经有一个值,所以 X = X + 1 行不能试图为X赋一个新值,因此该行是尝试进行模式匹配( 15 = 15 +1 ),这将始终失败。

Things are pretty simple with Erlang's single assignment: because X already has a value, the line X = X + 1 cannot be an attempt to assign a new value to X, so that line is an attempt to pattern match (15 = 15 + 1), which will always fail.

另一方面,在Elixir中变量不是单个赋值:

On the other hand, in Elixir variables are not single assignment:

Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> x = 15
15

iex(2)> x = 100
100

iex(3)> x
100

iex(4)> 

Elixir中变量不是单一赋值的事实意味着Elixir在编写时需要做出选择:

The fact that variables are not single assignment in Elixir means that Elixir needs to make a choice when you write:

x = 10
x = x + 1  #or even x = 15

选择1)第二行是否应解释为对 x 的赋值?

选项2)是否应将第二行解释为尝试进行模式匹配(即 10 = 11 )?

Choice 1) Should the second line be interpreted as assignment to x?
Choice 2) Should the second line be interpreted as an attempt to pattern match (i.e. 10 = 11)?

Elixir与Choice 1一起使用。这意味着实际上要用Elixir中所谓的模式匹配运算符执行模式匹配更加困难:您必须使用pin运算符( ^ )和匹配运算符( = ):

Elixir goes with Choice 1. That means that actually performing a pattern match with the so called pattern match operator in Elixir is more difficult: you have to use the pin operator(^) in conjunction with the match operator(=):

x = 10
^x = x + 1  

现在,第二行将总是失败。如果您想在不使用pin运算符的情况下执行模式匹配,还有一种技巧可以在某些情况下起作用:

Now, the second line will always fail. There is also a trick that will work in some situations if you want to perform pattern matching without using the pin operator:

x = 10
12 = x

在第二行中,将变量放在右侧侧。我认为规则可以这样表示:在模式匹配运算符( = )的右侧,总是对变量求值,即用其值替换。始终在左侧分配变量-除非使用pin运算符,否则在这种情况下,固定变量将替换为其当前值,然后在右侧进行模式匹配。结果,将Elixir的 = 运算符称为混合分配/模式匹配运算符可能更准确。

In the second line, you put the variable on the right hand side. I think the rule can be stated like this: On the right hand side of the pattern match operator(=), variables are always evaluated, i.e. replaced with their values. On the left hand side variables are always assigned to--unless the pin operator is used, in which case a pinned variable is replaced by its current value and then pattern matched against the right hand side. As a result, it's probably more accurate to call Elixir's = operator a hybrid assignment/pattern match operator.

这篇关于为什么x = x +1在Elixir中有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆