Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 18, 2012, 09:49 AM   #1
Anteeks
macrumors newbie
 
Join Date: Feb 2012
Prolog question

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?
Anteeks is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mophie Juice Pack Plus (iPhone 5) Question - Mute Button Question JulesK iPhone Accessories 3 Mar 30, 2014 03:58 PM
Snow Leopard File Vault Question and Basic System Password Question & Time Machine? GordonGekko999 Mac Basics and Help 0 Oct 25, 2013 07:06 AM
Question for anyone who has seen or owns the pink ipod nano? Color deciding question. Pinkstiletto66 iPod 4 Apr 4, 2013 01:57 PM
Some Prolog Help. Sir Night Train Mac Programming 0 Oct 10, 2012 11:33 PM
Family Share Question, Upgrade Question, Argh head hurting.. Jazwire iPhone 1 Sep 14, 2012 01:19 AM

Forum Jump

All times are GMT -5. The time now is 02:19 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC