Chronology Current Month Current Thread Current Date
[Year List] [Month List (current year)] [Date Index] [Thread Index] [Thread Prev] [Thread Next] [Date Prev] [Date Next]

Re: The world's first readable calculus [long]



Can you translate the algorithm into Fortran, or True Basic or Pascal?
I am probably not the only one who can not follow your steps in Perl.
Thanks in advance.

John Trammell wrote:

I agree with the book's answer; I wrote a little perl program
to generate and check the truth table. The output is

A B C op(A) op(B) op(C) agrees?
----- ----- ----- ----- ----- ----- -------
false false false false false false yes
false false true false true false no
false true false false true true no
false true true false false true no
true false false false true false no
true false true false false false no
true true false false false true no
true true true true false true no

where the first three columns are the 'states' of the 3 men
('false' means 'liar'), the next three columns are the
correctness of the opinions held by the men ('false' means
that their voiced opinion does not match the known state
of the three men), and the last column indicates agreement,
i.e. are all persons who are liars actually lying, and vice
versa.

The script follows. Note that it is (relatively) easily
generalized to arbitrary numbers of people and opinions.
Note also that arbitrary numeric values of 'true' and
'false' generate the correct truth table. :-)

Regards,
John

==================== 3dudes.pl ====================
#!/usr/bin/perl -w

#--------------------------------------------------------
# define truth and falsity
#--------------------------------------------------------

$false = 0;
$true = 1;

#--------------------------------------------------------
# define array containing both truth and falsity
#--------------------------------------------------------

@truefalse = ($false,$true);

#--------------------------------------------------------
# define string versions of truth and falsity
#--------------------------------------------------------

$s{$false} = 'false';
$s{$true} = 'true';

#--------------------------------------------------------
# define functions that describe the 3 men's
# opinions about truth and falsity
#
# A: "There are 3 truthtellers here".
# B: "No, only 1 of us is a truthteller."
# C: "The second person is telling the truth."
#
# each function below returns 'true' if the
# three inputs agree with the given person's
# statement, false otherwise
#
#--------------------------------------------------------

sub op_A {
local($A,$B,$C) = @_;
return(
(($A == $true) and ($B == $true) and ($C == $true)) ? $true : $false
);
}

sub op_B {
local($A,$B,$C) = @_;
return(
(
(($A == $true) and ($B == $false) and ($C == $false)) or
(($A == $false) and ($B == $true) and ($C == $false)) or
(($A == $false) and ($B == $false) and ($C == $true))
) ? $true : $false
);
}

sub op_C {
local($A,$B,$C) = @_;
return(($B == $true) ? $true : $false);
}

#--------------------------------------------------------
# see who's right
#--------------------------------------------------------

print STDOUT " A B C op(A) op(B) op(C) agrees?\n";
print STDOUT "----- ----- ----- ----- ----- ----- -------\n";

foreach $A (@truefalse) {
foreach $B (@truefalse) {
foreach $C (@truefalse) {

$t{'A'} = &op_A($A,$B,$C); # evaluate A's opinion
$t{'B'} = &op_B($A,$B,$C); # evaluate B's opinion
$t{'C'} = &op_C($A,$B,$C); # evaluate C's opinion

printf STDOUT "%5s %5s %5s ", $s{$A}, $s{$B}, $s{$C};

printf STDOUT "%5s ", $s{$t{'A'}};
printf STDOUT "%5s ", $s{$t{'B'}};
printf STDOUT "%5s ", $s{$t{'C'}};

if (($t{'A'} == $A) and ($t{'B'} == $B) and ($t{'C'} == $C)) {
print STDOUT " yes";
} else {
print STDOUT " no";
}

print STDOUT "\n";

}
}
}