Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a Prolog question (small problem)

Status
Not open for further replies.

Ante2

Programmer
Nov 18, 2012
1
0
0
SE
I need some help with a prolog question. Almost done with everything, just need help with a small pronlem.

Question is to make a cooking program. I have some ingredients at home, and some recipes.
Database has the following information:

at_home([ingredient(rice,2), ingredient(water,10)]).

recipe(boiled_rice,[ingredient(rice,4),ingredient(salt,3),ingredient(water,5)]).
recipe(chicken,[ingredient(chicken,2),ingredient(salt,1),ingredient(pepper,1)]).
recipe(saltwater,[ingredient(salt,1),ingredient(water,3)]).
recipe(noodles, [ingredient(noodles,2)|I1]) :- recipe(saltwater,I1).

Question is:

Q1: Cook(X); should tell me what I can cook (from the recipes) from the ingredients I have at home.

Q2: Buy(X, L): should tell me the amount of ingregients I need to buy for dish X.

My solution:

at_home([ingredient(rice,2), ingredient(water,10)]).

recipe(boiled_rice,[ingredient(rice,4),ingredient(salt,3),ingredient(water,5)]).
recipe(chicken,[ingredient(chicken,2),ingredient(salt,1),ingredient(pepper,1)]).
recipe(saltwater,[ingredient(salt,1),ingredient(water,3)]).
recipe(noodles, [ingredient(noodles,2)|I1]) :- recipe(saltwater,I1).

cook(X) :- at_home(YS), recipe(X,XS), check(XS,YS).

check([],_).
check([X|XS],YS) :- enough(X,YS), check(XS,YS).

enough(ingredient(V,T1),[ingredient(V,T2)|_]) :- T1 =< T2.
enough(I,[_|YS]) :- enough(I,YS).

buy(X, L) :- at_home(YS), recipe(X,XS), need(XS, YS, L).

need([], _, []).
need([X|XS], YS, L1) :- need(XS, YS, L), tobuy(X, YS, R), ( R = [], L1 = L, ! ; L1 = [R | L]).


tobuy(ingredient(V,T1), [], ingredient(V, T1)).

tobuy(ingredient(V, T1), [ingredient(V,T2)|_], R) :-
!, (T1>T2, L is T1-T2,
R = ingredient(V, L)
;
R = [])
.
tobuy(X, [ingredient(_,_)|TS], R) :- tobuy(X, TS, R).



Problem:

Q2 doesn’t work. It gives multiple answers for the same right. Example:

3 ?- buy(X,L).
X = boiled_rice,
L = [ingredient(rice, 2), ingredient(salt, 3)] ;
X = boiled_rice,
L = [ingredient(salt, 3)] ;

If it requires more rice and salt to cook boiled_rice then it shouldn.t at the same time require only more salt.

What have I done wrong?
 
The first question : I think that check is too much complicated :
Code:
check([], _).

check([ingredient(Name, Q) | T], Ys) :-
	member(ingredient(Name, Q1), Ys),
	Q =< Q1,
	check(T, Ys).
Second question : same problem
Code:
% we add the missing ingredient or not sufficient
buy(X, L) :-
	at_home(YS),
	recipe(X,XS),
	% at the beginning the list is empty
	need(XS, YS, [], L).

% when the list of ingredients is finished
% we unify the current and final list of
% needed ingredients
need([], _, L, L).

% ingredient at home, test if we have enough
need([ingredient(Name, Q) | T], Ys, Current_lst, Final_lst) :-
	member(ingredient(Name, Q1), Ys),
	% Have we enough ?
	(   Q =< Q1
	->  % yes nothing to add
	    New_lst = Current_lst
	;   % no we compute the quantity
	    Need is Q - Q1, 
	    % and we add the ingredient in the list
	    New_lst = [ingredient(Name, Need) | Current_lst]),
	need(T, Ys, New_lst, Final_lst).

% we have not the ingredient at home
need([ingredient(Name, Q) | T], Ys, Current_lst, Final_lst) :-
	\+member(ingredient(Name, _Q1), Ys), !,
	need( T, Ys, [ingredient(Name, Q) | Current_lst], Final_lst).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top