I am trying to get my head round some basic erlang functionality and I could do with some comments on the following.
I have the following erlang code that takes a list of tuples and returns a list minus an element if a key is found:
delete(Key, Database) ->
remove(Database, Key, []).
remove([], Key, Acc) ->
Acc;
remove([H|T], Key, Acc) ->
if
element(1, H) /= Key ->
[H| remove(T, Key, Acc)];
true ->
remove(T, Key, Acc)
end.
Is this a good way of doing this?
The if statement seems incorrect.
Also is my use of the accumulator Acc making this tail recursive?
No, your usage of
Accdoesn’t make it tail recursive. Your branch of if returns[H| remove(T, Key, Acc)]which is not tail call and this branch would be used most of time. To be more precise your usage ofAccis useless because it would be[]whole time, you don’t change its value at all. Correct code should look like.But if your list members are always pairs I would prefer direct pattern match:
But I think this is example where can apply Myth: Tail-recursive functions are MUCH faster than recursive functions so I would use much simpler recursive version: