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 IamaSherpa 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 task.

Status
Not open for further replies.

berghallen

Programmer
Oct 25, 2009
7
SE
I have write a program in Prolog, that will look in a list of ingredients and compare if you need to buy or what you can cook with all the ingredients you got home.

I have finish the first one that looks if I can cook for example rise. But I need to write a program that calculate how much I must buy to be able to cook the food.


The code so far, and I know that everything under buy isn't correct.

at_home([ingredient(rice,2), ingredient(water,10), ingredient(salt, 4), ingredient(pepper, 9), ingredient(milk, 1), ingredient(curry, 4)]).

recipe(rice,[ingredient(rice,4),ingredient(salt,3),ingredient(water,5)]).
recipe(chicken,[ingredient(chicken,2),ingredient(salt,1),ingredient(pepper,1)]).
recipe(currymix,[ingredient(curry,3),ingredient(salt,1),ingredient(pepper,2),ingredient(water,2)]).


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

kollacook([],_).
kollacook([X|XS],YS) :- tillrackligt(X,YS), kollacook(XS,YS).

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





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

kollabuy([], _, _).
kollabuy([X|XS], YS, L) :- kopa(X, YS, L), kollabuy(XS, YS, L).

kopa(ingredient(_, X), [], L) :- L is X.
kopa(ingredient(V, T1), ingredient(V,T2)|_, L) :- T1>T2, L is T1-T2.
kopa(X, ingredient(_,_)|TS, L) :- kopa(X, TS, L).


 
You must add in a list the ingredients you need.

this code is wrong :
Code:
kopa(ingredient(_, X), [], L) :- L is X.
No : when the homelist is empty, you have not foud the ingredient, you need to buy it and know its name.
kopa(Ingredient, [], Ingredient).

No, same error you need to know the name of the ingredient and also if there no need to buy it
kopa(ingredient(V, T1), [ingredient(V,T2)|_], R) :- 
   !, % the cut because the ingredient is found, no backtrack
   (T1>T2, % if there is not enough of the ingredient
   L is T1-T2,
   R = ingredient(V, L)
   ;
   % else R is empty,  don't forget to test the result of kopa
   R = [])
   .
Yes
kopa(X, ingredient(_,_)|TS, L) :- kopa(X, TS, L).


 
Thanks for reply, but I'm not getting it to work :/

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

kollabuy([], _, _).
kollabuy([X|XS], YS, L) :- kopa(X, YS, L), kollabuy(XS, YS, L).

%No : when the homelist is empty, you have not foud the ingredient, you need to buy it and know its name.
kopa(Ingredient, [], Ingredient).

%No, same error you need to know the name of the ingredient and also if there no need to buy it
kopa(ingredient(V, T1), [ingredient(V,T2)|_], R) :-
!, % the cut because the ingredient is found, no backtrack
(T1>T2, % if there is not enough of the ingredient
L is T1-T2,
R = ingredient(V, L)
;
% else R is empty, don't forget to test the result of kopa
R = [])
.
%Yes
kopa(X, ingredient(_,_)|TS, L) :- kopa(X, TS, L).
 
Code:
kollabuy([X|XS], YS, L) :- kopa(X, YS, R), kollabuy(XS, YS, L).
Here, you have to test R (third argument of kopa), is-it empty or not ?
 
Kollabuy is only there to look if any more ingredients need to be checked? And it is doing it in a recurive way?

Aint I correct?
 
Yes, but do you want to get a list of ingredients to buy ?
 
Yes but only for one ingredient, you gather the list of ingredients you need in L
 
Well, you have 2 possibilities, one with an accumulator, you unify this accumulator with the final list result, or you can built the list result at the return of recursion e.g.
Code:
% when recipe list is empty, list of ingredients is empty
kollabuy([], _, []).

% else I add the ingredient (or nothing) to the list 
% obtain at the return of the recursion
kollabuy([X|XS], YS, L1) :-
	kollabuy(XS, YS, L),
	kopa(X, YS, R),
	(   R = [], L1 = L, ! ; L1 = [R | L]).
Should work.
 
Sorry but it wont work :( The task is to show everything I need to buy to cook for exempel rise.

The code:

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

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


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

kopa(ingredient(V, T1), [ingredient(V,T2)|_], R) :-
!, (T1>T2, L is T1-T2,
R = ingredient(V, L)
;
R = [])
.
kopa(X, ingredient(_,_)|TS, R) :- kopa(X, TS, R).
 
There is an error here
Code:
kopa(X, ingredient(_,_)|TS, R) :- kopa(X, TS, R).
look at clause 2 of kopa.

 
Getting an error now:

8 ?- buy(X, L).
ERROR: >/2: Arguments are not sufficiently instantiated


And the code looks like this now.

kopa(X, [ingredient(_,_)|TS], R) :- kopa(X, [ingredient(_,_)|TS], R).
 
Code:
kopa(X, ingredient(_,_)|TS, R) :- kopa(X, TS, R).
should be
Code:
kopa(X, [ingredient(_,_)|TS], R) :- kopa(X, TS, R).
 
Hello, iam relatively new to prolog an i want to ask you why a program of mine doesnt work.
collapse is supposed to take a list of lists and find the list with all the members of the list of lists for example:
collapse([[1,2],[3,4]],[1,2,3,4]) = true.

collapse([[]],[]).
collapse([[LH]|[[LT]]],L2):- append([LH],L2,L3),collapse([[LT]],L3).

It seems really strange to me that my program doesnt work thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top