<br>
'''Background''': The '''man or boy test''' was proposed by computer scientist [[wp:Donald_Knuth|Donald Knuth]] as a means of evaluating implementations of the [[:Category:ALGOL 60|ALGOL 60]] programming language. The aim of the test was to distinguish compilers that correctly implemented "recursion and non-local references" from those that did not.

<blockquote style="font-style:italic">
  I have written the following simple routine, which may separate the 'man-compilers' from the 'boy-compilers'<br> &mdash; <span style="font-style:normal">Donald Knuth</span></blockquote>

'''Task''': Imitate [[#ALGOL 60 - Knuth's example|Knuth's example in Algol 60]] in another language, as far as possible. 

'''Details''': Local variables of routines are often kept in [http://c2.com/cgi/wiki?ActivationRecord ''activation records''] (also ''call frames''). In many languages, these records are kept on a [[System stack|call stack]]. In Algol (and e.g. in [[Smalltalk]]), they are allocated on a [[heap]] instead. Hence it is possible to pass references to routines that still can use and update variables from their call environment, even if the routine where those variables are declared already returned. This difference in implementations is sometimes called the [[wp:Funarg_problem|Funarg Problem]].

In Knuth's example, each call to ''A'' allocates an activation record for the variable ''A''. When ''B'' is called from ''A'', any access to ''k'' now refers to this activation record. Now ''B'' in turn calls ''A'', but passes itself as an argument. This argument remains bound to the activation record. This call to ''A'' also "shifts" the variables ''x<sub>i</sub>'' by one place, so eventually the argument ''B'' (still bound to its particular
activation record) will appear as ''x4'' or ''x5'' in a call to ''A''. If this happens when the expression ''x4 + x5'' is evaluated, then this will again call ''B'', which in turn will update ''k'' in the activation record it was originally bound to. As this activation record is shared with other instances of calls to ''A'' and ''B'', it will influence the whole computation.

So all the example does is to set up a convoluted calling structure, where updates to ''k'' can influence the behavior
in completely different parts of the call tree.

Knuth used this to test the correctness of the compiler, but one can of course also use it to test that other languages can emulate the Algol behavior correctly. If the handling of activation records is correct, the computed value will be &minus;67.

'''Performance and Memory''': Man or Boy is intense and can be pushed to challenge any machine. Memory (both stack and heap) not CPU time is the constraining resource as the recursion creates a proliferation activation records which will quickly exhaust memory and present itself through a stack error.  Each language may have ways of adjusting the amount of memory or increasing the recursion depth. Optionally, show how you would make such adjustments.

The table below shows the result, call depths, and total calls for a range of ''k'':
{| class="wikitable" style="font-size: 85%"
! ''k''
! 0
! 1
! 2
! 3
! 4
! 5
! 6
! 7
! 8
! 9
! 10
! 11
! 12
! 13
! 14
! 15
! 16
! 17
! 18
! 19
! 20
! 21
! 22
! 23
! 24
! 25
! 26
! 27
! 28
! 29
! 30
|-
! ''A''
|align="right"| 1
|align="right"| 0
|align="right"| -2
|align="right"| 0
|align="right"| 1
|align="right"| 0
|align="right"| 1
|align="right"| -1
|align="right"| -10
|align="right"| -30
|align="right"| -67
|align="right"| -138
|align="right"| -291
|align="right"| -642
|align="right"| -1,446
|align="right"| -3,250
|align="right"| -7,244
|align="right"| -16,065
|align="right"| -35,601
|align="right"| -78,985
|align="right"| -175,416
|align="right"| -389,695
|align="right"| -865,609
|align="right"| -1,922,362
|align="right"| -4,268,854
|align="right"| -9,479,595
|align="right"| -21,051,458
|align="right"| -46,750,171
|align="right"| -103,821,058
|align="right"| -230,560,902
|align="right"| -512,016,658
|-
! ''A'' called
|align="right"| 1
|align="right"| 2
|align="right"| 3
|align="right"| 4
|align="right"| 8
|align="right"| 18
|align="right"| 38
|align="right"| 80
|align="right"| 167
|align="right"| 347
|align="right"| 722
|align="right"| 1,509
|align="right"| 3,168
|align="right"| 6,673
|align="right"| 14,091
|align="right"| 29,825
|align="right"| 63,287
|align="right"| 134,652
|align="right"| 287,264
|align="right"| 614,442
|align="right"| 1,317,533
|align="right"| 2,831,900
|align="right"| 6,100,852
|align="right"| 13,172,239
|align="right"| 28,499,827
|align="right"| 61,786,266
|align="right"| 134,202,509
|align="right"| 292,011,464
|&nbsp;
|&nbsp;
|&nbsp;
|-
! ''A'' depth
|align="right"| 1
|align="right"| 2
|align="right"| 3
|align="right"| 4
|align="right"| 8
|align="right"| 16
|align="right"| 32
|align="right"| 64
|align="right"| 128
|align="right"| 256
|align="right"| 512
|align="right"| 1,024
|align="right"| 2,048
|align="right"| 4,096
|align="right"| 8,192
|align="right"| 16,384
|align="right"| 32,768
|align="right"| 65,536
|align="right"| 131,072
|align="right"| 262,144
|align="right"| 524,288
|align="right"| 1,048,576
|align="right"| 2,097,152
|align="right"| 4,194,304
|align="right"| 8,388,608
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|-
! ''B'' called
|align="right"| 0
|align="right"| 1
|align="right"| 2
|align="right"| 3
|align="right"| 7
|align="right"| 17
|align="right"| 37
|align="right"| 79
|align="right"| 166
|align="right"| 346
|align="right"| 721
|align="right"| 1,508
|align="right"| 3,167
|align="right"| 6,672
|align="right"| 14,090
|align="right"| 29,824
|align="right"| 63,286
|align="right"| 134,651
|align="right"| 287,263
|align="right"| 614,441
|align="right"| 1,317,532
|align="right"| 2,831,899
|align="right"| 6,100,851
|align="right"| 13,172,238
|align="right"| 28,499,826
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|-
! ''B'' depth
|align="right"| 0
|align="right"| 1
|align="right"| 2
|align="right"| 3
|align="right"| 7
|align="right"| 15
|align="right"| 31
|align="right"| 63
|align="right"| 127
|align="right"| 255
|align="right"| 511
|align="right"| 1,023
|align="right"| 2,047
|align="right"| 4,095
|align="right"| 8,191
|align="right"| 16,383
|align="right"| 32,767
|align="right"| 65,535
|align="right"| 131,071
|align="right"| 262,143
|align="right"| 524,287
|align="right"| 1,048,575
|align="right"| 2,097,151
|align="right"| 4,194,303
|align="right"| 8,388,607
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|&nbsp;
|}
<br>
;Related tasks:
* &nbsp; [[Jensen's Device]]
<br>

