Let say I have a list [1, 2, 3, 4]
How can I get all elements from this list except last? So, I'll have [1, 2, 3]
Let say I have a list [1, 2, 3, 4]
How can I get all elements from this list except last? So, I'll have [1, 2, 3]
If you're looking to get both the last item and the rest of the list preceding it you can now use List.pop_at/3:
{last, rest} = List.pop_at([1, 2, 3], -1)
{3, [1, 2]}
Erlang
List = [1,2,3,4,5],
NewList = DropLast(List).
DropLast(List) while length(List) > 0 and is_list(List) ->
{NewList, _} = lists:split(OldList, length(OldList)-1),
NewList.