Why is this variable unused?

后端 未结 1 1764
独厮守ぢ
独厮守ぢ 2021-02-07 03:34

Why does compiling this code:

triples( [], _,_,_)->
  [];

triples( Self, X, Y, none )->
  [ Result || Result = { X, Y, _} <- Self ].

相关标签:
1条回答
  • 2021-02-07 04:22

    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.

    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 ]
    
    0 讨论(0)
提交回复
热议问题