[M3devel] Man or Boy test

Randy Coleburn rcoleburn at scires.com
Thu May 14 21:23:01 CEST 2009


Martin:
 
I've only scanned your code briefly here, but I would suspect that you are running out of stack space due to infinite recursion.
 
I may be wrong here, but I think you have an infinite loop.
 
Reason is that in B() you call A() again, but in the call to A() you also recursively call B() as one of the parameters to A().  Since all the parameters have to be evaluated before you can call A(), you wind up calling B() recursively with no termination condition since the test for k<=0 only occurs in A(), not B().
 
Regards,
Randy Coleburn

>>> Martin Bishop <martinbishop at bellsouth.net> 5/14/2009 2:25 PM >>>
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.


CONFIDENTIALITY NOTICE:  This email and any attachments are intended solely for the use of the named recipient(s). This e-mail may contain confidential and/or proprietary information of Scientific Research Corporation.  If you are not a named recipient, you are prohibited from making any use of the information in the email and attachments.  If you believe you have received this email in error, please notify the sender immediately and permanently delete the email, any attachments, and all copies thereof from any drives or storage media and destroy any printouts of the email or attachments.

EXPORT COMPLIANCE NOTICE:  This email and any attachments may contain technical data subject to U.S export restrictions under the International Traffic in Arms Regulations (ITAR) or the Export Administration Regulations (EAR).  Export or transfer of this technical data and/or related information to any foreign person(s) or entity(ies), either within the U.S. or outside of the U.S., may require export authorization by the appropriate U.S. Government agency prior to export or transfer.  In addition, technical data may not be exported or transferred to certain countries or specified designated nationals identified by U.S. embargo controls without prior export authorization.  By accepting this email and any attachments, all recipients confirm that they understand and will comply with all applicable ITAR, EAR and embargo compliance requirements.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20090514/b2bc904e/attachment-0002.html>


More information about the M3devel mailing list