What does something like this (t ++ [d]) do

what does something like this do
(t ++ [d])

and why use
[d] instead of d

and finally, why the parentheses…

unfortunately the code is not documented as it seems that if you cain't read ELIXIR, you cain.t read.... so documentation ain't needed.

thank you for your help.

Hi,

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?

if t and d are lists then why use [d] instead of d since t does not have the brackets [[]
???

Kernel.++/2 only works with lists as arguments.

t ++ d won’t work (not exactly, you’ll end with an improper list)

EDIT:

Yeah, I was editing my post at the same time and hesitate if, for the PO, this “detail” would be confusing and worths mentioning it.

1 Like

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.

2 Likes

this is the complete function:

defp add_when_in(t, k, l, d) do

if k in l do
  (t ++ [d])
else
  t
end

end

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… :sunny:

You were right, parentheses are not required here.

when testing the [] ++ :wut I get:

LC-210722 inside add_when_in with ([] ++ :wut) lists

LC-210712 my data: :wut

so now, what does this mean?

Thanks again

PS: I am reading the article: Making sense of Elixir (improper) lists (dorgan.ar).

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.

humm, food for thought, will play with this a bit more…

thanks again,

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.

2 Likes

at the risk of sounding like a twit, what is O(n). I seen this before but don’t know what it means…

apologies for such a dumb question but might as well get it out of the way once and for all, LoL

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.

2 Likes

Can you give us an idea what are you trying to do?

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…