I'm trying to write a program in Prolog, that does two things:
- Takes two arguments, an integer and a list of integers
- Is satisfied if change for the first argument can be made from some combination of elements from the second
I have come up with a solution to this problem that allows a change rule to unify to coin facts, as such:
coin(quarter, 25).
coin(dime,10).
coin(nickel,5).
coin(penny,1).
change(0, []).
change(A, [(C, Num)|L]) :-
coin(C, V),
A >= V,
Num is floor(A / V),
B is A mod V,
change(B, L).
Additionally, if you pass something like change(27,L) to the interpreter, it generates all possible combinations of quarters, dimes, nickels, and pennies that can be used to make change, like such:
?- change(27,L).
L = [(quarter, 1), (penny, 2)] ;
L = [(dime, 2), (nickel, 1), (penny, 2)] ;
L = [(dime, 2), (penny, 7)] ;
L = [(nickel, 5), (penny, 2)] ;
L = [(penny, 27)] ;
false.
I'm having trouble, however, with how this can be solved by simply passing a currency list like [25,10,5,1] into change, making the call look something like change(27, [25,10,5,1], L). Is this possible and if so, how can it be done?