Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary List to String List - Erlang

Tags:

erlang

How do I convert [<<"a">>, <<"b">>, <<"c">>] to ["a", "b", "c"]?

like image 270
sura2k Avatar asked Dec 27 '11 09:12

sura2k


People also ask

How do I create a list in Erlang?

In Erlang, Lists are created by enclosing the values in square brackets.

How do I find the length of a list in Erlang?

You can use length() to find the length of a list, and can use list comprehensions to filter your list. num(L) -> length([X || X <- L, X < 1]). Working example: % list counter program -module(listcounter).

What is binary in Erlang?

Binaries store data in a much more space efficient manner than in lists or tuples, and the runtime system is optimized for the efficient input and output of binaries. Binaries are written and printed as sequences of integers or strings, enclosed in double less than and greater than brackets.

Is string an Erlang?

Strings are enclosed in double quotes ("), but is not a data type in Erlang.


2 Answers

[binary_to_list(X) || X <- [<<"a">>, <<"b">>, <<"c">>]]. or more elaborate

BinList = [<<"a">>, <<"b">>, <<"c">>],
NormalList = [binary_to_list(X) || X <- BinList],
NormalList.
like image 80
Muzaaya Joshua Avatar answered Sep 23 '22 07:09

Muzaaya Joshua


Or, using lists:map/2:

lists:map(fun erlang:binary_to_list/1, [<<"a">>, <<"b">>, <<"c">>]).
like image 30
Alin Avatar answered Sep 24 '22 07:09

Alin