Reputation: 1
In erlang, I want to merge two list as the following
when, A = [1, 2, 3, 4], B= ["A1", "A2", "A3", A4],
wanted result [{1, "A1"}, {2, "A2"}, {3, "A3"}, {4, A4}]
I've tried the following
''' - module(test). - export([start/0]).
start() ->
Abc = [2,3,1,4],
Bbc=["f1", "f2", "f3",f4],
ct:pal("Hello ~n"),
ct:pal("make_tuple_list ~p~n", [make_tuple_list(Abc,Bbc)]).
make_tuple_list([H1 | T1], [H2 | T2]) ->
[_ | _] = [{H1, H2} | make_tuple_list(T1, T2)].
make_tuple_list([], []) -> [].
''' but got the systax erorr as the following
test.erl:14: function make_tuple_list/2 already defined
thanks in advance.
Upvotes: 0
Views: 319
Reputation: 78
-module(lc).
-export([start/0]).
start() ->
Columns = ["id", "firstname", "lastname", "prefix", "initials", "birthday"],
Rows = [
[82, "Bob", "Dubalina", "", "B.", {1971,1,29}],
[45, "Alice", "Wonderland", "", "A.", {1975,2,14}],
[23, "John", "Doe", "", "J.", {1982,5,5}],
[72, "Jane", "Doe", "", "J.", {1995,7,17}]
],
Data = #{ "data" => [
maps:from_list([ {lists:nth(X, Columns) , lists:nth(X, Values)} || X <- lists:seq(1, length(Columns))])
|| Values <- Rows ]
},
Data.
Upvotes: 0
Reputation: 1782
Try following....
-module(test).
-export([start/0]).
start() ->
Abc = [2,3,1,4],
Bbc=["f1", "f2", "f3",f4],
ct:pal("Hello ~n"),
ct:pal("make_tuple_list ~p~n", [make_tuple_list(Abc,Bbc)]).
make_tuple_list([H1 | T1], [H2 | T2]) ->
[{H1, H2} | make_tuple_list(T1, T2)];
make_tuple_list([], []) -> [].
Separator for splitting function clauses is ;
. .
is function definition terminator. In the above case, both occurrences of make_tuple_list
are ending with .
, which essentially means, in the second occurrence we are re-defining an already defined function, which is not allowed in ErLang.
Upvotes: 1