First-class functions/Use numbers analogously: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Pascal draft) |
||
Line 1,205: | Line 1,205: | ||
};</syntaxhighlight> |
};</syntaxhighlight> |
||
The two are very similar, though as requested the test numbers are in 6 variables instead of two vectors. |
The two are very similar, though as requested the test numbers are in 6 variables instead of two vectors. |
||
=={{header|Pascal}}== |
|||
Works with FPC (currently only version 3.3.1). |
|||
<syntaxhighlight lang="pascal"> |
|||
program FunTest; |
|||
{$mode objfpc} |
|||
{$modeswitch functionreferences} |
|||
{$modeswitch anonymousfunctions} |
|||
uses |
|||
SysUtils; |
|||
type |
|||
TMultipler = reference to function(n: Double): Double; |
|||
function GetMultipler(a, b: Double): TMultipler; |
|||
var |
|||
prod: Double; |
|||
begin |
|||
prod := a * b; |
|||
Result := function(n: Double): Double begin Result := prod * n end; |
|||
end; |
|||
var |
|||
Multipler: TMultipler; |
|||
I: Integer; |
|||
x, xi, y, yi: Double; |
|||
Numbers, Inverses: array of Double; |
|||
begin |
|||
x := 2.0; |
|||
xi := 0.5; |
|||
y := 4.0; |
|||
yi := 0.25; |
|||
Numbers := [x, y, x + y]; |
|||
Inverses := [xi, yi, 1.0 / (x + y)]; |
|||
for I := 0 to High(Numbers) do begin |
|||
Multipler := GetMultipler(Numbers[I], Inverses[I]); |
|||
WriteLn(Multipler(0.5)); |
|||
end; |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.0000000000000000E-001 |
|||
5.0000000000000000E-001 |
|||
5.0000000000000000E-001 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |