Higher-order functions: Difference between revisions

Add Draco
(→‎{{header|Quackery}}: simplified fold)
(Add Draco)
Line 1,124:
<pre>[ 2 4 10 ]</pre>
 
=={{header|Draco}}==
<lang draco>/* Example functions - there are no anonymous functions */
proc nonrec square(word n) word: n*n corp
proc nonrec cube(word n) word: n*n*n corp
 
/* A function that takes another function.
* Note how a function is defined as:
* proc name(arguments) returntype: [code here] corp
* But a function variable is instead defined as:
* proc(arguments) returntype name
*/
proc nonrec do_func(word start, stop; proc(word n) word fn) void:
word n;
for n from start upto stop do
write(fn(n):8)
od;
writeln()
corp
 
/* We can then just pass the name of a function as an argument */
proc main() void:
do_func(1, 10, square);
do_func(1, 10, cube)
corp</lang>
{{out}}
<pre> 1 4 9 16 25 36 49 64 81 100
1 8 27 64 125 216 343 512 729 1000</pre>
=={{header|E}}==
<lang e>def map(f, list) {
2,095

edits