Jensen's Device: Difference between revisions

Content deleted Content added
Added R code
m copy edit - wiki link to wikipedia
Line 1: Line 1:
{{wikipedia}}
{{wikipedia}}
{{task|Classic CS problems and programs}}
{{task|Classic CS problems and programs}}
This task is an exercise in [http://en.wikipedia.org/wiki/Call-by-name#Call_by_name call by name].
This task is an exercise in [[wp:Call-by-name#Call_by_name|call by name]].


'''Jensen's Device''' is a computer programming technique devised by Danish computer scientist [[wp: Jørn_Jensen|Jørn Jensen]] after studying the [[ALGOL 60]] Report.
'''Jensen's Device''' is a computer programming technique devised by Danish computer scientist [[wp:Jørn_Jensen|Jørn Jensen]] after studying the [[ALGOL 60]] Report.


The following program was proposed to illustrate the technique. It computes the 100th [http://en.wikipedia.org/wiki/Harmonic_number harmonic number]:
The following program was proposed to illustrate the technique. It computes the 100th [[wp:Harmonic_number|harmonic number]]:


'''begin'''
'''begin'''
Line 25: Line 25:
'''end'''
'''end'''


The above exploits [http://en.wikipedia.org/wiki/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 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'',
Moreover, the ''first'' parameter to ''sum'',
Line 34: Line 34:
in this case ''i'', as the formal parameter.)
in this case ''i'', as the formal parameter.)


[http://en.wikipedia.org/wiki/Donald_Knuth Donald Knuth] later proposed the [[Man or boy test|Man or Boy Test]] as a more rigorous exercise.
[[wp:Donald_Knuth|Donald Knuth]] later proposed the [[Man or boy test|Man or Boy Test]] as a more rigorous exercise.
=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
Line 507: Line 507:


=={{header|R}}==
=={{header|R}}==
R uses a [http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_need call by need] evaluation strategy where function inputs
R uses a [[wp:Evaluation_strategy#Call_by_need|call by need]] evaluation strategy where function inputs
are lazily evaluated and then cached; functions can implement
are lazily evaluated and then cached; functions can implement
nonstandard evaluation such as call-by-name by using the "substitute" primitive to
nonstandard evaluation such as call-by-name by using the "substitute" primitive to