问题
I'm new to prolog.
Consider the following format:
carsCompany(Tel_Number,
Manager,
Company_Name,
[new_cars(Car_Name,info(Color,Creator),Date_Creation)],
[old_cars(Car_Name,info(Color,Creator),Date_Creation)]
]).
I would like to create a two argument relation which gets a list of car names and a company and adds them to the company.
update_company([Mazda],Company).
I have the following db example:
carsCompany(1234,
Jujiro Matsuda,
Mazda,
[new_cars(mazda_3,info(Grey,Person1),26082016)],
[old_cars(Cosmo,info(Black,Person2),26081927),
[old_cars(RX-7,info(Black,Person2),26081979),]
]).
What I tried to do is to get all the the possible cars using member
so I could iterate over the information.
member(X, [X|Xs]).
member(X, [Y|Ys]) :- member(X, Ys).
get_all(New,Old,carsCompany(_,_,_,New_Cars,Old_Cars)) :- member(New,New_Cars), member(Old, Old_Cars).
Although It doesn't really work:
New = [Lecture|_G750],
Old = [Tutorial|_G753] ;
New = [Lecture|_G750],
Old = [_G752, Tutorial|_G756] ;
What is the best way to implement this?
回答1:
Possible solution:
car_company( 1, 1234, person('Jujiro', 'Matsuda'), 'Mazda' ).
production( 1, cars, new, 'Mazda-3', info( gray, person1 ), 26/08/2016 ).
production( 1, cars, old, 'Cosmo', info( black, person2 ), 26/08/1927 ).
production( 1, cars, old, 'RX-7', info( black, person2 ), 26/08/1979 ).
% query:
q :-
findall(
new_car( ID, Model, Color ),
(
car_company( ID, _ , person('Jujiro', 'Matsuda'), 'Mazda' ),
production( ID, cars, new, Model, info( Color, _ ), _ )
),
Result1
),
writeq( Result1 ),
findall(
old_car( ID, Model, Color ),
(
car_company( ID, Phone, person('Jujiro', 'Matsuda'), 'Mazda' ),
production( ID, cars, old, Model, info( Color, _ ), _ )
),
Result2
),
writeq( Result2 ).
来源:https://stackoverflow.com/questions/51047005/iteration-over-relations-using-a-database