First-class functions: Difference between revisions
Content deleted Content added
→{{header|Perl 6}}: italicize functions; add some explanation |
Added BBC BASIC |
||
Line 173: | Line 173: | ||
Output: |
Output: |
||
<lang Axiom>[0.5,0.5,0.5]</lang> |
<lang Axiom>[0.5,0.5,0.5]</lang> |
||
=={{header|BBC BASIC}}== |
|||
{{works with|BBC BASIC for Windows}} |
|||
Strictly speaking you cannot return a ''function'', but you can return a ''function pointer'' which allows the task to be implemented. |
|||
<lang bbcbasic> REM Create some functions and their inverses: |
|||
DEF FNsin(a) = SIN(a) |
|||
DEF FNasn(a) = ASN(a) |
|||
DEF FNcos(a) = COS(a) |
|||
DEF FNacs(a) = ACS(a) |
|||
DEF FNcube(a) = a^3 |
|||
DEF FNroot(a) = a^(1/3) |
|||
dummy = FNsin(1) |
|||
REM Create the collections (here structures are used): |
|||
DIM cA{Sin%, Cos%, Cube%} |
|||
DIM cB{Asn%, Acs%, Root%} |
|||
cA.Sin% = ^FNsin() : cA.Cos% = ^FNcos() : cA.Cube% = ^FNcube() |
|||
cB.Asn% = ^FNasn() : cB.Acs% = ^FNacs() : cB.Root% = ^FNroot() |
|||
REM Create some function compositions: |
|||
AsnSin% = FNcompose(cB.Asn%, cA.Sin%) |
|||
AcsCos% = FNcompose(cB.Acs%, cA.Cos%) |
|||
RootCube% = FNcompose(cB.Root%, cA.Cube%) |
|||
REM Test applying the compositions: |
|||
x = 1.234567 : PRINT x, FN(AsnSin%)(x) |
|||
x = 2.345678 : PRINT x, FN(AcsCos%)(x) |
|||
x = 3.456789 : PRINT x, FN(RootCube%)(x) |
|||
END |
|||
DEF FNcompose(f%,g%) |
|||
LOCAL f$, p% |
|||
f$ = "(x)=" + CHR$&A4 + "(&" + STR$~f% + ")(" + \ |
|||
\ CHR$&A4 + "(&" + STR$~g% + ")(x))" |
|||
DIM p% LEN(f$) + 4 |
|||
$(p%+4) = f$ : !p% = p%+4 |
|||
= p%</lang> |
|||
'''Output:''' |
|||
<pre> |
|||
1.234567 1.234567 |
|||
2.345678 2.345678 |
|||
3.456789 3.456789 |
|||
</pre> |
|||
=={{header|Bori}}== |
=={{header|Bori}}== |