Concat/appending lists

Got a question about when to concat vs. prepending items to list then reversing to achieve appending.

So i know lists boil down to [1 | [2 | []]]. I also know that i can append to a list using something such as

iex(1)> list = [1, 2, 3]
[1, 2, 3]
iex(2)> list ++ [4]
[1, 2, 3, 4]

and I can prepend to a list such as

iex(3)> [4 | list]
[4, 1, 2, 3]

But to achieve what I did before on the line above, i would have to reverse the list, prepend, then reverse again. What if i tried to append a list of items? why would the fastest way be to use something such as Enum.concat or using the ++ to stitch together the two lists and not doing a recursive call that will do just prepend the bits and flip the entire list?

1 Like