[erlang-questions] ++ operator

Michał Ptaszek erlang@REDACTED
Wed Oct 31 18:24:15 CET 2012


Hey,

I ran into a weird bug in my system (R15B02), namely I was trying to
append a non-list item to a tail of the list by running:
List ++ Item

Nonetheless, as long as the list is empty the result of this expression is:
[] ++ Item == Item

For instance:
3> [] ++ {a, b}.
{a,b}

On the other hand,
5> a ++ [].
** exception error: bad argument
     in operator  ++/2
        called as a ++ []

Does Erlang have a built-in optimizations such as?:
++([], B) -> B;
++(A, B) -> lists:append([A, B]).

If so, it would be nice to add a guard:
++([], B) when is_list(B) -> B;
++(A, B) -> lists:append([A, B]).

Kind regards,
Michal Ptaszek



More information about the erlang-questions mailing list