Coding Erlang

Posted in erlang; comments: 0

For the past year or so I've been trying to learn a new language called Erlang.

I've found this nice document, 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:

1 int main(int c) {
2     printf("This is nice");
3     exit(1);
4 }

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:

1 int main(int c) {
2     printf("This is nice");
3     exit(1);
4 }

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.

Tags: erlang

0 comments

Comments are closed

If you really, really want to comment, please mail miek@miek.nl.

0 comments in moderator queue