问题描述
为什么编译此代码:
triples( [], _,_,_)->
[];
triples( Self, X, Y, none )->
[ Result || Result = { X, Y, _} <- Self ].
报告:
./simple_graph.erl:63: Warning: variable 'X' is unused
./simple_graph.erl:63: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'X' is unused
./simple_graph.erl:64: Warning: variable 'X' shadowed in generate
./simple_graph.erl:64: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'Y' shadowed in generate
并返回错误的结果:full self。
And return wrong result: full Self.
推荐答案
这是因为变量发生在发电机的LHS上,X和Y在这里,总是新的未绑定变量本地到理解。这意味着它们与三元组头中的X和Y不是相同的变量,因此,没有隐含的等式测试。这类似于乐趣,其中所有变量发生在乐趣的头部都是本地的乐趣的新变量。
This is because variables occurring on the LHS of generators, X and Y here, are always new unbound variables local to the comprehension. This means that they are not the same variables as the X and Y in the head of triples and, therefore, there is no implicit equality test. This similar to funs where all variables occurring in the head of a fun are alse new variables local to the fun.
这与其他大部分的erlang不同,是为什么编译器不仅警告头部中的X和Y不被使用,而且X和Y在理解中影响其他变量。他们在理解中也没有任何地方使用。
This is different from most of the rest of erlang, which is why the compiler not only warns that the X and Y in the head are not used but also that the X and Y in the comprehension shadow the other variables. They are also unused anywhere in the comprehension.
一个简单的方法来获得你想要的东西:
An easy way to get what you want is:
[ Result || Result = {X1,Y1,_} <- Self, X =:= X1, Y =:= Y1 ]
这篇关于为什么Erlang变量未使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!