prolog recursion
Posted
by
AhmadAssaf
on Stack Overflow
See other posts from Stack Overflow
or by AhmadAssaf
Published on 2011-01-02T02:03:39Z
Indexed on
2011/01/03
6:54 UTC
Read the original article
Hit count: 242
am making a function that will send me a list of all possible elemnts .. in each iteration its giving me the last answer .. but after the recursion am only getting the last answer back .. how can i make it give back every single answer ..
thank you
the problem is that am trying to find all possible distributions for a list into other lists .. the code
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)).
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 and instead of Or .. i get error :
ERROR: is/2: Arguments are not sufficiently instantiated
appreciate the help ..
Thanks alot @hardmath
© Stack Overflow or respective owner