0

我想将元素附加到列表中,并且不允许使用列表库或任何其他 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}]

我理解错误消息,但我不知道如何从这里开始......

4

3 回答 3

2

我怀疑这只是Db2已经有一个值的情况,它与返回值不同db:write[{apple,fruit},{cucumber,vegetable}]根据错误消息)。键入Db2.以查看它具有什么值,然后键入f(Db2).以“忘记”它的值,以便您可以再次分配给它。

于 2013-01-18T13:55:14.233 回答
2

您可以通过以下方式将元素附加到列表中List ++ [Element]

于 2013-01-18T14:15:02.480 回答
1

不建议使用 ++ 运算符附加。您应该只将它用于小列表。

你有两个选择: - lists:append (你说这不是一个选项)或 - 你可以用 | 放在列表前面 操作员。

于 2015-01-19T08:03:48.150 回答