我想將元素追加到列表中,並且我不允許使用列表庫或任何其他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! –