我想将元素追加到列表中,并且我不允许使用列表库或任何其他BIF。我怎么想它的一个例子是:将元素追加到没有Erlang中的列表库模块的列表中
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}]
我理解的错误消息,但我不知道如何从这里走......
修好了!问题不在于我的解决方案,就像你说的,Db2已经被分配了一个值,并且在重新启动shell时,它工作正常!感谢legoscia! –