Prolog: Choosing NOT to insert an element during list recursion -
I have made a bill which takes 2 lists as logic and with the product of "recipes" Adds the list, although I need to create a third rule that refuses to insert the product if the product at hand is an empty list.
Then the first list can look like this:
recipe ([component (rice, 4), component (salt, 3), component (water, 5)] ).
and the other is:
content ([ingredient (rice, 3), constituent (salt, 4), constituent (water, 4)) ,
At this time it comes back: list = [ingredient (rice, 1), [], component (water, 1)]
I want to return it to: list = [ingredient (rice, 1), component (water, 1)]
need_to_buy ([], _ , List): - list = []. Need_to_buy ([H | HS], [X | X], [Difference list]: - H = ingredient (type, amount), recipes (type, zodiac, [x | x], difference), need_to_buy (hs, [ X | Xs], list).
Below is how far I've got with the solution.
recipes (type, type, sum, [], difference): - difference = ingredient (type, amount). Recipes (type, zodiac, [z | _], difference): - Z = component (type, stock), quantity & gt; Stock, newamount zodiac - stock, difference = component (type, newamount). Recipes (type, zodiac, [z | _], difference): - Z = ingredient (type, stock), stock & gt; Recipes (type, zodiac, [z | z], difference): - Z = ingredient (wrong type, _), wrong type \ = type, recipes (type, zodiac, z, difference). I usually do not have a bunch of nested conditions but this "felt right" at this time, and this is the solution I found: need_to_buy ([], _, []). Need_to_buy ([Component (type, amount needed) | Materials], onhand, required): - Do we have any hands? Member (Ingredient (type, zodiachandhand), onhand) - & gt; % If the quantity of hand is more than the required amount, then calculate and repeat the required amount, except for the remaining% (AmountOnHand> = AmountNeeded -> Need_to_buy (content, onhand, required)% otherwise; (AmountToBuy Zodiac - Required - amount, need _to_bay (content, onhand, restanded), required = [component (type, zodiaca) | near the rest])% If we do not have any hands, we only need to request Can use the amount, and iterate; Ne Ed_to_buy (Content, Ohand, Restated), required = [Component (type, amount needed) | Rest Required]
Otherwise I think you have very unskilled testing and re- I'm looking into my code that the main mistake is that you are matching patterns on the second argument.
or MemberCrack / 2
is easy to rely on To do the dirty work of finding the right ingredient in the stuff in your hand. If I have done this with a section, instead it probably looks like this:
need_to_buy ([], _, []).% Case 1: We do not have all components on need_to_buy ([Component (type, amount needed). Content], onhand, [ingredient (type, sum) is required): - + + member (component (type, _), onhand), need_to_buy (content, onhand, required). % Case 2: We have this, but do not need enough _to_buy ([Component (type, amount required) | Material], Ohand, [Ingredient (type, zodiac ticket) | RestNeeded]): - Member (component, Type AmountOnHand), Onhaand), Radical & gt; Zodiachandhand, Zodiac Twin Amount is required - Zodiachandhand, Needed_buy (Material, Onhand, Restanded). % Case 3: We have enough need _to_buy ([Component (type, amount required) | Content], Onhand, Restored): - Member Scroll (component, zodiac sign), onhaand), zodiac = 0; Zodiachandhand, need_to_buy (content, onhand, restanded)
It leaves an option point on the stack and generally looks like the same conditions change again and again for my taste. But if it looks better for you, then it should also work.
Comments
Post a Comment