Jensen's Device: Difference between revisions
Content added Content deleted
(complex code, rounded result to simpler code and real result) |
(+Icon+Unicon) |
||
Line 272: | Line 272: | ||
main = print foo</lang> |
main = print foo</lang> |
||
Output: 5.187377517639621 |
Output: 5.187377517639621 |
||
== Icon and Unicon == |
|||
Traditional call by name and reference are not features of Icon/Unicon. Procedures parameters are passed by value (immutable types) and reference (mutable types). However, a similar effect may be accomplished by means of co-expressions. The example below was selected for cleanliness of calling. |
|||
==={{header|Icon}}=== |
|||
<lang Icon>record mutable(value) # record wrapper to provide mutable access to immutable types |
|||
procedure main() |
|||
A := mutable() |
|||
write( sum(A, 1, 100, create 1.0/A.value) ) |
|||
end |
|||
procedure sum(A, lo, hi, term) |
|||
temp := 0 |
|||
every A.value := lo to hi do |
|||
temp +:= @^term |
|||
return temp |
|||
end</lang> |
|||
Refreshing the co-expression above is more expensive to process but to avoid it requires unary alternation in the call. |
|||
<lang Icon> write( sum(A, 1, 100, create |1.0/A.value) ) |
|||
... |
|||
temp +:= @term</lang> |
|||
Alternately, we can use a programmer defined control operator (PDCO) approach that passes every argument as a co-expression. Again the refresh co-expression/unary iteration trade-off can be made. The call is cleaner looking but the procedure code is less clear. Additionally all the parameters are passed as individual co-expressions. |
|||
<lang Icon> write( sum{A.value, 1, 100, 1.0/A.value} ) |
|||
... |
|||
procedure sum(X) |
|||
... |
|||
every @X[1] := @X[2] to @X[3] do |
|||
temp +:= @^X[4]</lang> |
|||
==={{header|Unicon}}=== |
|||
This Icon solution works in Unicon. |
|||
=={{header|J}}== |
=={{header|J}}== |