How to add an item to an existing list without creating a new variable in erlang?

I have a list containing some elements, and now with the help of lists: foreach I retrieve a few more entries and I want to add each value to my existing list elements without creating a new variable, like in other languages ​​using an array, I am new to erlang pls, someone helps me with this. Here is my sample code that I get ... exception error: matching right side value [6,7,1].

Code example:

listappend() ->
    A = [1,2,3,4,5],
    B = [6,7],
    lists:foreach(fun (ListA) ->
        B = lists:append(B, [ListA])                       
        end, A),
    B.

I need a conclusion like

B = [6,7,1,2,3,4,5].

thank

+3
source share
1 answer

First of all, this function already exists, so you do not need to implement it yourself. In fact, a list can take two lists as arguments:

1> lists:append([1,2,3,4,5], [6,7]).
[1,2,3,4,5,6,7]

:

2> [1,2,3,4,5] ++ [6,7]. 
[1,2,3,4,5,6,7]

, , ++ , . , , , "cons" ( , ):

3> [1|[2,3,4,5,6,7]].
[1,2,3,4,5,6,7]

, , , . , A B , my_append/2.

my_append(A, B) ->
  YOUR_CODE_GOES_HERE

, , - :

B = lists:append(B, [ListA])

B, [6,7].

+4
source

All Articles