it is a merge of 2 lists: you append the list [d] (a list of one element, d) to t which is a list. There are several ways to write the same thing (for example: List.insert_at(t, -1, d))
why the parentheses…
Can’t say without the rest of the code. To avoid a conflict (precedence) with an other operator?
d needs to be wrapped in a list if it’s a single element, otherwise you’ll end up with an improper list [1, 2, 3 | d]
I go over the fine details here: Making sense of Elixir (improper) lists (dorgan.ar). The tl;dr is that is has to do with the way lists are constructed. [1, 2, 3] is actually [1 | [2 | [3 | []]]], and all the ++ operator does is replace the last [] with whatever you have at the right side of the operator. Because the tail of a list needs to be a list or an empty list to be a proper list, you need to wrap a non-list element in a list to not break the data structure.
That’s partially correct. It only checks that the left side is a list, the right side can be whatever. Try doing [] ++ :wut for a nice surprise.
Thank you for all of your answers, it’s amazing how a simple list can complicate matters depending on how it’s used. in my case it was the naming convention or the lack of it that threw me for a loop.
Thanks guys not only for clear and prompt description but for being there.
Now I can apply this new found knowledge to refactor the code and move on…
The ++ operator looks for the last [] in the list and replaces it with the value at the right side without checking if it’s also a list. So if you give it [] at the left, it will “replace” it with the value at the right, so you get the right side value and no list.
Keep in mind that ++ is O(n) so if you try to build up a non trivial list by adding elements to the end, one at a time, it is most likely not the right way.
It means the run time of appending is proportional to the the length of the first list. If you are repeatably adding element at the end, then the run time will be 0 + 1 + 2 + … + n = n*(n-1)/2 or O(n^2)
On the other hand, [d | t] is O(1) constant time. So if you are going to build the same list from scratch, you add to the head, then do a reverse (O(n) and one time) in the end to get the proper order. the run time will be still O(n)
If you still don’t understand, you better check some wikipedia or text books.
at the time I did not any idea what this code was doing, now I understand it but it’s not something I would use, it’s not clear to some in the group work with. obviously the person that wrote that code is no longer here and the people hare now say “oh he was just crazy”
Crazy… anywho: thanks for the responses, great group…