我有以下用于客户创建和列表的代码:
:-dynamic customer/2.
load:-consult('C:\\customers.txt').
save:-tell('C:\\customers.txt'), listing(customer), told.
%New customer
new_customer:-write("Name: "), read(Name),
customer_code(Code), asserta(customer(Code, Name)), save.
customer_code(Code):- customer(C, _, _, _), Code is C + 1.
customer_code(1).
到目前为止,一切都很好。问题是,当尝试进行更复杂的搜索、过滤和报告时,我不得不使用retract
来清理客户的当前内存状态。
因此,在任何列表之前,我倾向于再次查阅文件(调用load
):
list_customers:- load, listing(customer).
这里的问题是,这种新的负载会导致listing
重复添加到数据库中的最后一个客户。
例如:
C:\customers.txt:
:-dynamic customers/2
(2, 'John')
(1, 'Alicia')
清单(客户):
(2, 'John')
(2, 'John')
(1, 'Alicia')
我已经能够通过retractall
在咨询之前使用来避免这种情况:
load:- reatractall(customer(_,_)), consult('C:\\customers.txt').
这是一个好/坏的做法吗?我不太明白这里发生了什么或为什么这能解决问题。