Higher-order functions: Difference between revisions

m
→‎{{header|Phix}}: added get_routine_info() comment, Phix/basics libheader, syntax coloured
m (added whitespace.)
m (→‎{{header|Phix}}: added get_routine_info() comment, Phix/basics libheader, syntax coloured)
Line 2,980:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<lang Phix>procedure use(integer fi, a, b)
<!--<lang Phix>(phixonline)-->
?fi(a,b)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">use</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fi</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #0000FF;">?</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function add(integer a, b)
return a + b
<span style="color: #008080;">function</span> <span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">b</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
use(add,23,45)</lang>
<span style="color: #000000;">use</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">45</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
{{out}}
<pre>
68
</pre>
You could also use get_routine_info(fi) to retrieve the maximum and minimum number of parameters, along with a string representing the signature in roottype format, and the actual name of the routine. Above it would return {2,2,"FII","add"} indicating add is a function that takes two integers, none of which are optional. Obviously you would need to invoke functions and procedures differently, eg you cannot print the result of a procedure call, likewise for different numbers and base types of parameters. Or as above just trust you were passed something appropriate, and rely on/expect a very clear human readable fatal error message if not.
 
The plain add, without a trailing '(' to make it a direct invocation, resolves to a symbol table index.<br>
Obviously you can use (an otherwise pointless) user defined type (of any name you like) instead of integer if preferred, eg
<!--<lang Phix>-->
<span style="color: #008080;">type</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*r*/</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">type</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">use</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rid</span> <span style="color: #000000;">fi</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)...</span>
<!--</lang>-->
 
=={{header|Phixmonti}}==
7,794

edits