Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

backtracking problem

Status
Not open for further replies.

ahmadassaf

Technical User
Jan 2, 2011
1
GB
hello ..
am trying to find all possible list permutations that satisfy a specific conditions .. i managed to do :

test :- bp(3,12,[7, 3, 5, 4, 6, 4, 5, 2], Answer),
format("Answer = ~w\n",[Answer]).
bp(NB,C,OL,A):-
addIn(C,OL,[[],[],[]],A);
bp(NB,C,_,A).
addIn(_,[],Result,Result).
addIn(C,[Element|Rest],[F|R],Result):-
member( Members , [F|R]),
sumlist( Members, Sum),
sumlist([Element],ElementLength),
Cap is Sum + ElementLength,
(Cap =< C,
append([Element], Members,New),
insert( Members, New, [F|R], PartialResult),
addIn(C,Rest,PartialResult,Result)).
insert(OldBin,NewBin,[OldBin|Rest],[NewBin|Rest]).
insert(OldBin,NewBin,[First|Rest1],[First|Rest2]):-
insert(OldBin,NewBin,Rest1,Rest2).

by calling test .. am getting back all the list of possible answers .. now if i tried to do something that will fail like

bp(3,11,[8,2,4,6,1,8,4],Answer).

it will just enter a while loop .. more over if i changed the

bp(NB,C,OL,A):-
addIn(C,OL,[[],[],[]],A);
bp(NB,C,_,A).

to ANDinstead of OR.. i get error :

ERROR: is/2: Arguments are not sufficiently instantiated

appreciate the help ..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top