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]

Three liars, in FORTRAN [long]



Hello all:

Ludwik Kowalski wrote:

On Mon, 07 Dec 1998 John Trammell wrote:

... Perl is an object-oriented language, but the script I wrote uses
none of those constructs. It's ideal for these "quick and dirty"
programs because it's not a compiled language (like C, FORTRAN,
C++, etc.) and yet it has a complete set of math functions unlike
other scripting languages.

Doesn't Fortran have these functions as well? Why should "compiled
versus interpreted" make any difference in a code which probably has
less than 50 statements. And why should a language in which your
algorithm was expressed make a difference? It is the algorithm, the
smart part, that I want to understand.

Compiled versus interpreted only makes a difference in the coding
cycle. Interpreted means a faster cycle, in my opinion. Most
interpreted languages that I deal with lack sin(), log(), etc., so
that leaves Perl. Quick and Dirty.

You created a program for a
problem which at least 5 out of 10 college students (and many
professors, myself included) would probably not be able to solve
without reading the answer provided by Jack.

Well, I am the Man. :-)

I also tried to create the algorithm but was not successful. Can you
describe the algorithm in a way which could help others to succeed?
Do it in plain English, or in a pseudo language, or in Fortran, etc.
How do you feed information to the program (three statements and
other constrains) and how do you tell the program what you want
in the output?

See below. The output of this guy is identical to the Perl script
I wrote. In English, I'd say my algorithm just searches every
available region of "truth space" (all combinations of lying/truth-
telling for all men) and checks to see if their stories agree.

Do you think that object oriented languages are more
appropriate for writing algorithms which solve syllogistic problems
than traditional languages?

Absolutely. The idea of a 'hash' (an arbitrary pairing of key/value
pairs, which is a built-in type in Perl) is such a basic concept that
working in languages (like FORTRAN and C) that lack one is like, say,
trying to write a calculus book in Hawaiian. :-)

A good language either has the guts to make it easy, or has enough
guts to let you build the guts to make it easy. The closest thing to
a hash in FORTRAN is a function (which is why I had to add function
'foo' in the code below).

Regards,
John

-------------------- three.f --------------------
c+
c FILENAME: three.f
c PURPOSE: to solve the "three men" problem
c AUTHOR: trammell (John Trammell)
c NOTE: to compile:
c f77 -o three three.f
c
c-

program three
implicit none

integer i,j,k
logical tf(2)
logical opa,opb,opc
character*5 s(2)

logical opina,opinb,opinc
external opina,opinb,opinc

character*5 foo
external foo

tf(1) = .false.
tf(2) = .true.

s(1) = 'false'
s(2) = 'true'

write(6,1001)
write(6,1002)

1001 format(' A B C op(A) op(B) op(C) agrees?')
1002 format('----- ----- ----- ----- ----- ----- -------')

do i=1,2
do j=1,2
do k=1,2

write(6,2001) s(i),s(j),s(k)

2001 format(A5,' ',A5,' ',A5,' ',$)

opa = opina(tf(i),tf(j),tf(k))
opb = opinb(tf(i),tf(j),tf(k))
opc = opinc(tf(i),tf(j),tf(k))

write(6,2001) foo(opa),foo(opb),foo(opc)

if(
1 (opa.eqv.tf(i)).and.(opb.eqv.tf(j)).and.(opc.eqv.tf(k))
2 )then
write(6,2002) 'yes'
else
write(6,2002) 'no'
endif

2002 format(A5)

enddo
enddo
enddo

stop
end

c-------------------------------------------------------------
c define mildly bogus logical -> string function
c-------------------------------------------------------------

character*5 function foo(a)
implicit none
logical a

if(a.eqv..true.)then
foo = 'true'
else
foo = 'false'
endif

return
end

c-------------------------------------------------------------
c define 'opinion' functions
c-------------------------------------------------------------

logical function opina(a,b,c)
implicit none
logical a,b,c

if((a.eqv..true.).and.(b.eqv..true.).and.(c.eqv..true.))then
opina = .true.
else
opina = .false.
endif

return
end

c-------------------------------------------------------------

logical function opinb(a,b,c)
implicit none
logical a,b,c

if (
1 ((a.eqv..true.) .and.(b.eqv..false.).and.(c.eqv..false.)).or.
2 ((a.eqv..false.).and.(b.eqv..true.) .and.(c.eqv..false.)).or.
3 ((a.eqv..false.).and.(b.eqv..false.).and.(c.eqv..true.))
4) then
opinb = .true.
else
opinb = .false.
endif

return
end

c-------------------------------------------------------------

logical function opinc(a,b,c)
implicit none
logical a,b,c

if (b.eqv..true.) then
opinc = .true.
else
opinc = .false.
endif

return
end