2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -22,13 +22,10 @@ The following program was proposed to illustrate the technique. It computes the
print (sum (i, 1, 100, 1/i))
'''end'''
The above exploits [[wp:Call-by-name#Call_by_name|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 every time the corresponding formal parameter's value was required. If the last parameter to ''sum'' had been passed by value, and assuming the initial value of ''i'' were 1, the result would have been 100 × 1/1 = 100.
The above exploits [[wp:Call-by-name#Call_by_name|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 in the caller's context every time the corresponding formal parameter's value was required. If the last parameter to ''sum'' had been passed by value, and assuming the initial value of ''i'' were 1, the result would have been 100 × 1/1 = 100.
Moreover, the ''first'' parameter to ''sum'',
representing the "bound" variable of the summation,
must also be passed by name, otherwise it would not be possible
to compute the values to be added.
(On the other hand, the global variable does not have to use the same identifier,
in this case ''i'', as the formal parameter.)
Moreover, the ''first'' parameter to ''sum'', representing the "bound" variable of the summation, must also be passed by name (or at least by reference), otherwise changes to it (made within ''sum'') would not be visible in the caller's context when computing each of the values to be added.
(On the other hand, the global variable does not have to use the same identifier, in this case ''i'', as the formal parameter.)
[[wp:Donald_Knuth|Donald Knuth]] later proposed the [[Man or boy test|Man or Boy Test]] as a more rigorous exercise.
<br><br>

View file

@ -0,0 +1,8 @@
FUNCTION SUM(I,LO,HI,TERM)
SUM = 0
DO I = LO,HI
SUM = SUM + TERM
END DO
END FUNCTION SUM
WRITE (6,*) SUM(I,1,100,1.0/I)
END

View file

@ -0,0 +1,21 @@
FUNCTION SUMJ(I,LO,HI,TERM) !Attempt to follow Jensen's Device...
INTEGER I !Being by reference is workable.
INTEGER LO,HI !Just as any other parameters.
EXTERNAL TERM !Thus, not a variable, but a function.
SUMJ = 0
DO I = LO,HI !The specified span.
SUMJ = SUMJ + TERM(I) !Number and type of parameters now apparent.
END DO !TERM will be evaluated afresh, each time.
END FUNCTION SUMJ !So, almost there.
FUNCTION THIS(I) !A function of an integer.
INTEGER I
THIS = 1.0/I !Convert to floating-point.
END !Since 1/i will mostly give zero.
PROGRAM JENSEN !Aspiration.
EXTERNAL THIS !Thus, not a variable, but a function.
INTEGER I !But this is a variable, not a function.
WRITE (6,*) SUMJ(I,1,100,THIS) !No statement as to the parameters of THIS.
END

View file

@ -0,0 +1,3 @@
fun sum(lo: Int, hi: Int, f: (Int) -> Double) = (lo..hi).sumByDouble(f)
fun main(args: Array<String>) = println(sum(1, 100, { 1.0 / it }))

View file

@ -0,0 +1,27 @@
{$MODE objFPC}
type
tTerm = function(i: integer):real;
function term(i:integer):real;
Begin
term := 1/i;
end;
function sum(var i: LongInt;
lo,hi: integer;
term:tTerm):real;
Begin
result := 0;
i := lo;
while i<=hi do begin
result := result+term(i);
inc(i);
end;
end;
var
i : LongInt;
Begin
writeln(sum(i,1,100,@term));
end.

View file

@ -1,10 +1,12 @@
/*REXX program to demonstrate Jensen's device (call sub, arg by name). */
numeric digits 50 /*might as well get some accuracy*/
/*REXX program demonstrates Jensen's device (via call subroutine, and args by name).*/
numeric digits 90 /*use 90 decimal digits (9 is default).*/
say sum( 'i', "1", '100', "1/i" ) /*invoke SUM (100th harmonic number).*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sum: procedure; parse arg j,start,finish,exp; $=0
say sum( 'i', "1", '100', "1/i" ) /*invoke SUM (100th harmonic num)*/
interpret 'do' j "=" start 'to' finish"; $=$+" exp '; end'
/* ──── ═ ─── ═════ ──── ══════────────── ═══ ───────── */
/* lit var lit var lit var literal var literal */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SUM subroutine──────────────────────*/
sum: procedure; parse arg i,start,finish,term; sum=0
interpret 'do' i "=" start 'to' finish"; sum=sum+" term '; end'
return sum
return $