Add new element to list

Elixir

Elixir Problem Overview


I was trying to add a new element into a list as follow:

iex(8)> l = [3,5,7,7,8] ++ 3
[3, 5, 7, 7, 8 | 3]
iex(9)> l
[3, 5, 7, 7, 8 | 3]

Why did I get on the 5th position like

8 | 3

What it does mean?
And how can I add new element to the list?

--------Update--------
I try to loop the list as follow:

iex(2)> l = [1,2] ++ 3
[1, 2 | 3]
iex(3)> Enum.each(l, fn(x) -> IO.puts(x) end)
1
2
** (FunctionClauseError) no function clause matching in Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:604: Enum."-each/2-lists^foreach/1-0-"(#Function<6.54118792/1 in :erl_eval.expr/5>, 3)
    (elixir) lib/enum.ex:604: Enum.each/2

Since the pointer of the number 2 is not pointing to a list, rather to value 3, how can I loop the list?

Elixir Solutions


Solution 1 - Elixir

Just follow the Elixir docs to add an element to a list ( and keep performance in mind =) ):

iex> list = [1, 2, 3]
iex> [0 | list]   # fast
[0, 1, 2, 3]
iex> list ++ [4]  # slow
[1, 2, 3, 4]

https://hexdocs.pm/elixir/List.html

Solution 2 - Elixir

The ++ operator is for concatenating two lists, then maybe what you want to do in order to add a new element is to put it within a list. Then, I think you should add the 3 into another list:

> iex(2)> l = [3,5,7,7,8] ++ [3] > > [3, 5, 7, 7, 8, 3]

Solution 3 - Elixir

First: [1, 2 | 3] is the notation for an improper list.

Second: To do the Enum.each you're trying to do with an improper list the code would look like this:

> Matching against proper/improper lists is correspondingly easy. So a > length function len for proper lists: > > len([|T]) -> 1 + len(T); len([]) -> 0. where we explicitly match for > the terminating []. If given an improper list this will generate an > error. While the function last_tail which returns the last tail of a > list can handle improper lists as well: > > last_tail([|T]) -> last_tail(T); last_tail(Tail) -> Tail.
> %Will match any tail

That is, of course, Erlang code from @rvirding. Translated to Elixir and translated to do the printing you give in your example, it'd look like this:

iex(6)> defmodule T do
...(6)>   defp print([h|t]) do
...(6)>     IO.puts(h)
...(6)>     print(t)
...(6)>   end
...(6)>   defp print(t) do
...(6)>     IO.puts(t)
...(6)>   end
...(6)>   def print_improper_list(il), do: print(il)
...(6)> end
iex:6: warning: redefining module T
{:module, T,
 <<70, 79, 82, 49, 0, 0, 5, 136, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 161, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
 {:print_improper_list, 1}}
iex(7)> T.print_improper_list([1,2,3|4])
1
2
3
4
:ok

I leave it as an exercise for you to figure out how to do that with an Enum.each.

Solution 4 - Elixir

Join by Enum.concat

Example:

iex> new_elem = 5
iex> Enum.concat([1, 2, 3], [new_elem])
[1, 2, 3, 5]

Solution 5 - Elixir

The "I" means that the list is divided into head and tail like so [head | tail]. If you pattern match the list this way you can manipulate both parts of the list and use the ++ operator for concatenation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsoftshipperView Question on Stackoverflow
Solution 1 - ElixirChilianView Answer on Stackoverflow
Solution 2 - ElixirSalvador MedinaView Answer on Stackoverflow
Solution 3 - ElixirOnorio CatenacciView Answer on Stackoverflow
Solution 4 - Elixirhana9View Answer on Stackoverflow
Solution 5 - ElixirSasha FonsecaView Answer on Stackoverflow