# Coding Erlang


For the past year or so I've been trying to learn a new language called
[Erlang](http://www.erlang.org). 

I've found this [nice
document](http://www.castro.aus.net/~maurice/serc/erlbk/), which can be
used when learning the language. So here I am on Saturday evening doing
some Erlang exercises :-)

Anyhow, I was doing exercise 3.4:

1. Erlang provides a function *lists:append* which joins two lists
   together, implement you own function *append* that performs the append
   operation. (**Do NOT peek**; the answer is given in figure 3.11).

And the answer given is:

CODE(erlang){
% Append 2 lists together
-module(append).
-export([append/2]).

append([], L) ->
    L;
append([H|T], L) ->
    [H, append(T, L)].
}CODE

However, when I test this I get:

    % erl
    1> c(append).
    {ok,append}

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

Which made me suspicious. Checking with the official *BIF*:

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

Hmm. With a little help from "Programming Erlang" I found the `++`
operator for adding lists together. So my final solution became:

CODE(erlang){
% Append 2 lists together
-module(append).
-export([append/2]).

append([], L) ->
        L;
append([H|T], L) ->
        [H] ++ append(T, L).
}CODE

Checking:

    4> c(append).                      
    {ok,append}
    5> append:append([4,5,6], [1,2,3]).
    [4,5,6,1,2,3]

The only ugly thing IMHO is the creation of the one element list with
`[H]` at the last line.

