ahmadassaf
Technical User
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 ..
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 ..