elixir - how to get all elements except last in the list?

Viewed 6491

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]

6 Answers

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.
Related