Home > Archive > Prolog > October 2004 > urgent prolog help...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
urgent prolog help...
|
|
|
| hi,
i'm new in the world of prolog. i have to make this Prolog predicate
comb(L,S,SL) where SL are al generated lists wish de sum is S and L is
de original list.
like this:
?- comb([1,4,3,2,5],7,D).
D = [1,4,2];
D = [4,3];
D = [2,5];
no
?- comb([1,2,3],7,D).
no
what i do have now is the following:
sum([],0).
sum([0|Ns],S) :-
sum(Ns,S).
sum([s(N)|Ns],s(S)) :-
sum([N|Ns],S).
comb(0,_,[]).
comb(N,[X|T],[X|Comb]):-
N>0,N1 is N-1,comb(N1,T,Comb).
comb(N,[_|T],Comb):-
N>0,comb(N,T,Comb).
comb2(_,[]).
comb2([X|T],[X|Comb]):-
comb2(T,Comb).
comb2([_|T],[X|Comb]):-
comb2(T,[X|Comb]).
i can do de following:
?- comb2([1,2,3,4,5,6],P), sum(P,7).
P=[1,2,4];
P=[1,6];
P=[2,5];
P=[3,4];
no.
but i have problems to make de right predicate as asked.
Thanks for any help.
| |
|
| mehdi,
the first help i can give is to revise your statement of the problem.
comb(L,S,SL) succeeds if SL is A sublist of L and the sum of all elements of
SL is S.
Note that SL is NOT ALL sublists of L for each successful. This is
important, so you can then restate the problem as:
comb(L,S,SL) :- sublist(L,SL), sum(SL,S).
Use the same problem-statement method for sublist and sum.
For example
sum(SL,S) succeeds if SL is empty and S is 0.
sum(SL,S) succeeds if SL has head H and tail T, sum of T is Tsum, and S is H
plus Tsum.
what alternatives do you have for sublist?
what about when L is empty?
when L is not empty, it has a Head and a Tail, and if Tail has a sublist
SLT....
happy prologing.
Walter
"mido" <mehdi_binl@hotmail.com> wrote in message
news:a35ca946.0410070613.2f72838d@posting.google.com...
> hi,
> i'm new in the world of prolog. i have to make this Prolog predicate
> comb(L,S,SL) where SL are al generated lists wish de sum is S and L is
> de original list.
> like this:
>
> ?- comb([1,4,3,2,5],7,D).
> D = [1,4,2];
> D = [4,3];
> D = [2,5];
> no
> ?- comb([1,2,3],7,D).
> no
>
> what i do have now is the following:
>
> sum([],0).
> sum([0|Ns],S) :-
> sum(Ns,S).
> sum([s(N)|Ns],s(S)) :-
> sum([N|Ns],S).
>
> comb(0,_,[]).
> comb(N,[X|T],[X|Comb]):-
> N>0,N1 is N-1,comb(N1,T,Comb).
> comb(N,[_|T],Comb):-
> N>0,comb(N,T,Comb).
>
> comb2(_,[]).
> comb2([X|T],[X|Comb]):-
> comb2(T,Comb).
> comb2([_|T],[X|Comb]):-
> comb2(T,[X|Comb]).
>
> i can do de following:
>
> ?- comb2([1,2,3,4,5,6],P), sum(P,7).
> P=[1,2,4];
> P=[1,6];
> P=[2,5];
> P=[3,4];
> no.
>
> but i have problems to make de right predicate as asked.
>
> Thanks for any help.
|
|
|
|
|