Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop last element in list Erlang

Tags:

erlang

I need to delete last element in list. I run this code in shell.

erl +pc unicode
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
ColumnListWithCommas = [<<"username">>,<<",">>,<<"password">>,<<",">>,
<<"created_at">>,<<",">>,<<"id">>,<<",">>,<<"email_hash">>,
<<",">>,<<"status">>,<<",">>,<<"mess_count">>,<<",">>].
  
lists:droplast(ColumnListWithCommas).
** exception error: undefined function lists:droplast/1
like image 680
max Avatar asked Jun 19 '14 12:06

max


People also ask

How do you remove the first element from a list in Elixir?

The easiest way to write it is actually Enum. drop(list, -1) .

How do I check if a list is Erlang empty?

This is the function that is called: set_viewer_values(Value, ViewerSet) -> if ViewerSet /= empty_set -> lists:map(fun(ViewerPid) -> ViewerPid ! {self(), set_value, Value} end, ViewerSet) end.

Is Erlang a list?

The List is a structure used to store a collection of data items. In Erlang, Lists are created by enclosing the values in square brackets. Following is a simple example of creating a list of numbers in Erlang.

How do I add to a list in Erlang?

In Elixir and Erlang we use `list ++ [elem]` to append elements.


1 Answers

There is no lists:droplast/1 in Erlang R16B03. you need Erlang 17.0

You can do:

1> A = [1, 2, 3, 4].
2> lists:reverse(tl(lists:reverse(A))).
[1,2,3]

Or

3> {L, _} = lists:split(length(A) - 1, A).

And one more way. You can take realization from Erlang sources it is pretty simple

like image 115
couchemar Avatar answered Sep 17 '22 23:09

couchemar