我很难理解Erlang中的尾递归。
我有以下的eunit测试:
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).
这是我的实现:
-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([]) ->
[].
我的实现是否是尾递归的?如果不是,我该如何使其成为尾递归?
提前感谢。
append
函数是否是尾递归的?我可以问一下它的作用吗?[Record|Database]
不也能很好地工作吗? - Samir Talwarappend([H|T]) -> T1 = append(T), [H|T1];
- Zed