×
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

Compare lists and return if true.

Compare lists and return if true.

Compare lists and return if true.

(OP)
Hey everyone, pretty new to Prolog and just started programming in it,hence the "newbie"-questions.

I decided to make a small software that, this is basic I know - but I have trouble getting started with it, anyways here's the example;

I'm a carpenter, wanting to build different things for my house so I have a few "recipes" for different stuff and their required ingredients. At home I also have a few ingredients, maybe not enough though.

My ingredients at home;
at_home([ingredient(nails,2), ingredient(wood,10)]).

and the "recipes";
recipe(wooden_roof,[ingredient(nails, 4), ingredient(hammer,1), ingredient(wood, 5)]).

recipe(stairs, [ingredient(nails, 2), ingredient(hammer,1), ingredient(wood, 2)]).

Now I want to be able to write ;
build(X) and then the software should return all the things I can build.

And the second part of the example is that I want a function (I've some programming in C# so I'm a bit damaged from that, lol)
called "buy" where it returns what I need to buy in order to be able to build everything, i.e;
buy(X,L)
And then it returns the ingredients needed to be bought.

Is there anyone who can help me with that? I really need a kick in the butt to get started.
Or do ya have any recommended tutorials?

RE: Compare lists and return if true.

What have you tried ?
 

RE: Compare lists and return if true.

(OP)
This is what I have tried so far;

buy(X,L) :- recipe(X,Ls), compare3(L,Ls).

compare3([], []).
compare3(K, [L|Ls]):- compareAtHome(L, at_home(Ks),K), compare3(L, Ls).



compareAtHome(ingredient(F1,S1),[ingredient(F2,S2)|Ks], L):- (F1==F2 -> to_buy(S1,S2, K), add(ingredient(F1,K),L,L1), compareAtHome(ingredient(F1,S1), [Ks]);compareAtHome(ingredient(F1,S1),[Ks])).

to_buy(X,Y,Z):- K is X-Y, K >= 0, Z is K.


add(X,[],[X]).
add(X,[A|L],[A|L1]):- add(X,L,L1).

RE: Compare lists and return if true.

Ok, I give you a solution in the manner you used, but there is a probleme, when an ingredient is not needed, the TT is set to 0, I'll give you a tip where when an ingredient is not needed it is not in the result list.

CODE

buy(X,L) :-
    recipe(X,Ls),
    compare3(L, Ls).

compare3([], []).

compare3([K | Ks], [L|Ls]):-
    compareAtHome(K, L),
    compare3(Ks, Ls).

% compareAtHome(ingredient(F,TT),ingredient(F,S))
% @arg1 : ingredent to buy (maybe)
% @arg2 : ingredient needed
compareAtHome(ingredient(F,TT),ingredient(F,S)):-
    at_home(Lst),
    (   member(ingredient(F, Nb), Lst) ->
        % the ingredient is present at home,
        % we check if we have enough (required S)
       (   Nb >= S -> % not needed we set the number to 0
                   TT = 0
       ;   TT is S - Nb
       )
    ;   % the ingredient is not at home
        % we need to buy all
        TT = S).
Now, if you don't want to have 0 is the list, you must walk through the list with compare3(LstNeeded, CurrentLstToBuy, FinalLstToBuy).

CODE

% the list is finished, we unify the 2 lists Current and Final
compare3([], LstToBuy, LstToBuy).

% here we study the current ingredient
compare3([ingredient(F, S) | T], CurrentLstToBuy, FinalLstToBuy) :-
........ what have we to do ????
   compare3(T, NewCurrentLstToBuy, FinalLstToBuy).
Hope this help !

RE: Compare lists and return if true.

What do you mean by "walk through the list with compare3(LstNeeded, CurrentLstToBuy, FinalLstToBuy)." could you show an example or write it in the code?

RE: Compare lists and return if true.

Walk through the list : example sum of a list with accumulator.

CODE

% list is finished
sum([], Acc, Acc).

% one step
sum([H|T], CurAcc, FinalAcc) :-
   Acc1 is CurAcc + H,
   % next step
   sum(T, Acc1, FinalAcc).

RE: Compare lists and return if true.

yea so i put "compare3([H|T], CurAcc, FinalAcc) :- Acc1 is CurAcc + H, sum(T, Acc1, FinalAcc)." after "compare3([], LstToBuy, LstToBuy)." but i still don't get it to work, what am i doing wrong?

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