Bonjour,
D'avance je vous préviens ; je suis très débutant en Prolog (ça ne fait qu'un mois que j'ai commencé à l'étudier).
Mon but est d'écrire combien de sucre est contenu dans une recette de cuisine. Pour cela, j'ai deux types de prédicats :
Le dernier argument du prédicat "ingrédient" correspond au nombre de grammes de sucre pour cet ingrédient.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 composition('Pain Indien chapati',farine,1). composition('Pain Indien chapati',ail,0.25). composition('Pain Indien chapati',huile,62.5). composition('Pain Indien chapati',sel,5). /* Ceux-là expriment les ingrédients contenus dans une recette */ ingredient(ail, 'ail', 1, 'g', 0.01). ingredient(farine, 'farine', 100, 'g', 0.41). ingredient(huile, 'huile', 1, 'mL', 0). ingredient(sel, 'sel', 1, 'pincée(s)',0). /* Ceux-là indiquent combien il y a de sucre dans chacun des ingrédients */
Et voici mes fonctions :
Et voici mon problème : Quand j'arrive dans
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 recette :- quantites_recette('Pain Indien chapati',Sucre). quantites_recette(Recette,Sucre) :- findall(Ingredient, composition(Recette,Ingredient,_),Ingredients), list_to_set(Ingredients,Ingredients2), quantites_ingredients(1,Ingredients2,Sucre), format('Nb sucre (2):~wg',Sucre). quantites_ingredients(DebutCalcul,[Ingredient|Ingredients],Sucre) :- ingredient(Ingredient,_,QteStandard,MesureStandard,Sucre2), composition(Recette,Ingredient,Qte), (DebutCalcul==1->(SucreTot is (Sucre2*Qte)) ;(SucreTot is (Sucre+(Sucre2*Qte)))), quantites_ingredients(0,Ingredients,SucreTot). quantites_ingredients(DebutCalcul,[],Sucre) :- format('Nb sucre (1):~wg',Sucre).
Il m'affiche :
Code : Sélectionner tout - Visualiser dans une fenêtre à part quantites_ingredients(DebutCalcul,[],Sucre).
C'est à dire qu'une fois qu'il est revenu dans la fonction appelante il a oublié le contenu de la variable. Je ne comprends pas pourquoi, et je ne sais pas comment y remédier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part Nb sucre :0.4125gNb sucre :_L125g
Merci de m'aider svp
Partager