#!/usr/bin/perl
use strict;
use warnings;
use AI::Prolog;
my @bills = qw(Fifties Twenties Tens Fives Ones);
my $bills = join ',', @bills; # don't use $prolog->list because these are vars
my $program = <<"END_PROLOG";
change([ $bills ]) :-
member(Fifties,[0,1,2]),
member(Twenties,[0,1,2,3,4,5]),
member(Tens,[0,1,2,3,4,5,6,7,8,9,10]),
member(Fives,[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]),
Total is 50*Fifties + 20*Twenties +10*Tens + 5*Fives,
Total <= 100,
Ones is 100 - Total.
member(X, [X|_]).
member(X, [_|Tail]) :-
member(X, Tail).
END_PROLOG
my $prolog = AI::Prolog->new($program);
$prolog->query("change([ $bills ]).");
while (my $result = $prolog->results) {
for my $i ( 0 .. $#bills ) {
print "$result->[1][$i] $bills[$i]";
print ", " unless $i == $#bills;
}
print "\n";
}
__END__
# Beginning of output:
0 Fifties, 0 Twenties, 0 Tens, 0 Fives, 100 Ones
0 Fifties, 0 Twenties, 0 Tens, 1 Fives, 95 Ones
0 Fifties, 0 Twenties, 0 Tens, 2 Fives, 90 Ones
0 Fifties, 0 Twenties, 0 Tens, 3 Fives, 85 Ones
0 Fifties, 0 Twenties, 0 Tens, 4 Fives, 80 Ones
0 Fifties, 0 Twenties, 0 Tens, 5 Fives, 75 Ones
0 Fifties, 0 Twenties, 0 Tens, 6 Fives, 70 Ones
0 Fifties, 0 Twenties, 0 Tens, 7 Fives, 65 Ones
0 Fifties, 0 Twenties, 0 Tens, 8 Fives, 60 Ones
...