I had the following code for customer creation and listing:
:-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).
So far, so good. The problem is, when trying to do more complex search, filtering and reports in general, I had to use retract
to clean the current memory state of the customers.
So, before any listing, I tend to consult the file again (calling load
):
list_customers:- load, listing(customer).
What goes wrong here is that more times than not, this new load will cause the listing
to repeat the last customer added to the database.
Eg:
C:\customers.txt:
:-dynamic customers/2
(2, 'John')
(1, 'Alicia')
listing(customers):
(2, 'John')
(2, 'John')
(1, 'Alicia')
I've been able to avoid this by using retractall
before consulting:
load:- reatractall(customer(_,_)), consult('C:\\customers.txt').
Is this a good/bad practice? I don't quite understand what's going on here or why this solves the problem.