I want to append elements to a list and I’m not allowed to use the lists library or any other BIF. An example of how I want it to be:
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}]
The code I have now for this (not working):
write(Key, Element, []) -> [{Key, Element}|[]];
write(Key, Element, [H|T]) -> [H|write(Key,Element,T)].
The error I’m getting is when I do this:
3> Db2 = db:write(cucumber, vegetable, Db1).
** exception error: no match of right hand side value [{apple,fruit},{cucumber,vegetable}]
I understand the error message but I dont know how to go from here…
I suspect that this is just a case of
Db2already having a value, and it’s a different value from the return value ofdb:write(which is[{apple,fruit},{cucumber,vegetable}]according to the error message). TypeDb2.to see what value it has, and typef(Db2).to “forget” its value, so that you can assign to it again.