So now it's trivial to find out if "marcus" is an average taxpayer (no) or if mary is (yes). You'd have to write a bit more Perl code than I would have to write Prolog code.Actually, you don't. It translates pretty straightforward:
You might want to declare some variables to make it strict (that would be extra code). About the only extra code the Perl part has is 2 extra spouse declarations, as hashes aren't symmetric. How does Prolog know that the "spouse" relationship is symmetric?$foreigner{abdul} = 1; $foreigner{marcus} = 1; $spouse{edward} = "sally"; $spouse{sally} = "edward"; $spouse{bill} = "hillary"; $spouse{hillary} = "bill"; $gross_income{hillary} = 100_000; $gross_income{edward} = 20_000; $gross_income{bill} = 30_000; $gross_income{marcus} = 49_999; $gross_income{mary} = 35_000; sub average_taxpayer {my $Person = @_ ? shift : $_; !$foreigner{$Person} and not ($spouse = $spouse{$Person} and $income1 = $gross_income{$spouse} and $income1 > 30_000) and $income2 = $gross_income{$Person} and $income2 < 50_000 }
However, this is the kicker. What if I want a list of all average taxpayers? Well, I know you have to have a gross income to be an average tax payer, so I issue the following query:And in Perl:
Now, I know about logic coding, and I'm aware of its merits. No need to convice me. But you've to come with a better example to show you need less code.@avg_taxpayers = grep {average_taxpayer} keys %gross_income;
In reply to Re^7: (why to use logic programming)
by Anonymous Monk
in thread Easy Text Adventures in Perl
by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |