×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need help with a Prolog question (small problem)

Need help with a Prolog question (small problem)

Need help with a Prolog question (small problem)

(OP)
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?

RE: Need help with a Prolog question (small problem)

The first question : I think that check is too much complicated :

CODE --> Prolog

check([], _).

check([ingredient(Name, Q) | T], Ys) :-
	member(ingredient(Name, Q1), Ys),
	Q =< Q1,
	check(T, Ys). 
Second question : same problem

CODE --> Prolog

% 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). 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close