Tail recursion in Erlang

Posted by dagda1 on Stack Overflow See other posts from Stack Overflow or by dagda1
Published on 2010-04-17T14:14:25Z Indexed on 2010/04/17 15:03 UTC
Read the original article Hit count: 378

Filed under:

Hi,

I am really struggling to understand tail recursion in Erlang.

I have the following eunit test:

db_write_many_test() ->
    Db = db:new(),
    Db1 = db:write(francesco, london, Db),
    Db2 = db:write(lelle, stockholm, Db1),
    ?assertEqual([{lelle, stockholm},{francesco, london}], Db2).

And here is my implementation:

-module(db) .
-include_lib("eunit/include/eunit.hrl").
-export([new/0,write/3]).

new() ->
    [].

write(Key, Value, Database) ->
    Record = {Key, Value},
    [Record|append(Database)].

append([H|T]) ->
    [H|append(T)];  
append([])  ->
    [].

Is my implementation tail recursive and if not, how can I make it so?

Thanks in advance

© Stack Overflow or respective owner

Related posts about erlang