RosettaCodeData/Task/Higher-order-functions/CLU/higher-order-functions.clu
2023-07-01 13:44:08 -04:00

24 lines
776 B
Text

% Functions can be passed to other functions using the 'proctype'
% type generator. The same works for iterators, using 'itertype'
% Here are two functions
square = proc (n: int) returns (int) return (n*n) end square
cube = proc (n: int) returns (int) return (n*n*n) end cube
% Here is a function that takes another function
do_calcs = proc (from, to: int, title: string,
fn: proctype (int) returns (int))
po: stream := stream$primary_output()
stream$putleft(po, title, 8)
stream$puts(po, " -> ")
for i: int in int$from_to(from,to) do
stream$putright(po, int$unparse(fn(i)), 5)
end
stream$putc(po, '\n')
end do_calcs
start_up = proc ()
do_calcs(1, 10, "Squares", square)
do_calcs(1, 10, "Cubes", cube)
end start_up