Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
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.

Report this thread to moderator Post Follow-up to this message
Old Post
mido
10-07-04 08:58 PM


Re: urgent prolog 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.



Report this thread to moderator Post Follow-up to this message
Old Post
W
10-08-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:50 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.