Is this behavior of ++ and -- intended?

Hello.
I found a strange behavior of ++ and --.

iex(1)> x = [:a, :b, nil, :c]
[:a, :b, nil, :c]
iex(2)> x -- [:a] -- [:c]
[:b, nil, :c]
iex(3)> x -- [:a] ++ [:c]
[:b, nil]

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])
[:b, nil]

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 and ++/2 are “right to left” associative, x -- [:a] -- [:c] is therefore equivalent to x -- ([:a] -- [:c]).

As [:a] without [:c] is still [:a], only :a gets removed from x then.

8 Likes

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.

https://erlang.org/doc/reference_manual/expressions.html#operator-precedence

1 Like

OK, thank you so much!

If you think about it is more efficient for ++. So 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.

Actually -- came later. It was a bit of a joke actually: if we have ++ couldn’t we have -- and ** and // as well? I couldn’t come up with something sensible for lists for ** and //. :smile: It also took a while to work out the semantics.

8 Likes

** could generate cross product of A and B. Because why not.

5 Likes