[M3devel] Man or Boy test

Martin Bishop martinbishop at bellsouth.net
Thu May 14 20:25:02 CEST 2009


I tried implementing Knuth's Man or Boy test in Modula-3, but I can't 
get it to work.

I assumed the code I had wouldn't work (and I was right), but eventually 
I fiddled enough and got it to compile, but now it just segfaults when run.

MODULE ManOrBoy EXPORTS Main;

IMPORT IO, Fmt;

PROCEDURE Zero(): INTEGER = BEGIN RETURN 0; END Zero;
PROCEDURE One(): INTEGER = BEGIN RETURN 1; END One;
PROCEDURE Neg(): INTEGER = BEGIN RETURN -1; END Neg;

PROCEDURE A(k:INTEGER; x1, x2, x3, x4, x5: INTEGER): INTEGER =

  PROCEDURE B(): INTEGER =
    BEGIN
      DEC(k);
      RETURN A(k, B(), x1, x2, x3, x4);
    END B;

  BEGIN
    IF k <= 0 THEN
      RETURN x4 + x5;
    ELSE
      RETURN B();
    END;
  END A;

BEGIN
  IO.Put(Fmt.Int(A(10, One(), Neg(), Neg(), One(), Zero())) & "\n");
END ManOrBoy.

All I know is that it segfaults at procedure B.



More information about the M3devel mailing list