我想将元素附加到列表中,并且不允许使用列表库或任何其他 BIF。我希望它如何的一个例子:
Eshell V5.9.1 (abort with ˆ G)
1> Db = db:new().
[]
2> Db1 = db:write(apple, fruit, Db).
[{apple,fruit}]
3> Db2 = db:write(cucumber, vegetable, Db1).
[{apple,fruit},{cucumber,vegetable}]
我现在拥有的代码(不起作用):
write(Key, Element, []) -> [{Key, Element}|[]];
write(Key, Element, [H|T]) -> [H|write(Key,Element,T)].
我得到的错误是当我这样做时:
3> Db2 = db:write(cucumber, vegetable, Db1).
** exception error: no match of right hand side value [{apple,fruit},{cucumber,vegetable}]
我理解错误消息,但我不知道如何从这里开始......