[go: nahoru, domu]

Jump to content

Jensen's device

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 216.251.209.111 (talk) at 18:04, 7 September 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.

The following program was proposed to illustrate the technique. It computes the 100th harmonic number:

  begin integer i;
     real procedure sum(i, lo, hi, term);
          value lo, hi; integer i, lo, hi; real term;
          comment term is passed by-name;
     begin real temp; temp := 0;
           for i := lo step 1 until hi do
              temp := temp + term;
           sum := temp
     end;
     print( sum(i, 1, 100, 1/i))
  end

The above exploits call by name to produce the correct answer (5.187...). It depends on the assumption that an expression passed as an actual parameter to a procedure would be re-evaluated everytime the corresponding formal parameter's value was required. If the last parameter to sum had been passed by value, the result would have been 100 × 1/1 = 100.

Donald Knuth later proposed the Man or Boy Test as a more rigorous exercise.