I found a strange behavior of
iex(1)> x = [:a, :b, nil, :c]
[:a, :b, nil, :c]
iex(2)> x -- [:a] -- [:c]
[:b, nil, :c]
iex(3)> x -- [:a] ++ [:c]
But a result I expected is this.
iex(4)> Kernel.--(x, [:a]) |> Kernel.++([:c])
[:b, nil, :c, :c]
iex(5)> Kernel.--(x, [:a]) |> Kernel.--([:c])
I think the calculation is processed one by one from the beginning of the expression. So the result should be same as latter one, I think.
How does the former one come?
++/2 are “right to left” associative,
x -- [:a] -- [:c] is therefore equivalent to
x -- ([:a] -- [:c]).
[:c] is still
:a gets removed from
Thank you for your answer!
Does the “right to left” associative have a merit? Or just it is defined so?
Not that I am aware of. Though I think it was just taken from erlang, such that it can be inlined without complex rewrite.
If you think about it is more efficient for
X ++ Y ++ Z will evaluate faster, less copying, if it is interpreted as
X ++ ( Y ++ Z ) than if it interpreted as
( X ++ Y ) ++ Z. Hence it is right associative.
-- was just given the same to try and be a little consistent.
-- came later. It was a bit of a joke actually: if we have
++ couldn’t we have
// as well? I couldn’t come up with something sensible for lists for
//. It also took a while to work out the semantics.
** could generate cross product of
B. Because why not.