fork download
  1. with Ada.Text_IO; use Ada.Text_IO;
  2.  
  3. procedure Main is
  4.  
  5. function Second_Order (L_Func, R_Func : access function (L, R : Integer) return Integer)
  6. return access function (L, R : Integer) return Integer is
  7.  
  8. function New_Function (L, R : Integer) return Integer is
  9. begin
  10. return L_Func (L, R) + R_Func (L, R);
  11. end New_Function;
  12.  
  13. begin
  14. return New_Function'Unrestricted_Access;
  15. end Second_Order;
  16.  
  17. function Plus (L, R : Integer) return Integer is
  18. begin
  19. return L + R;
  20. end Plus;
  21. function Mult (L, R : Integer) return Integer is
  22. begin
  23. return L * R;
  24. end Mult;
  25.  
  26. begin
  27. Ada.Text_IO.Put_Line (Integer'Image (Second_Order (Plus'Access, Mult'Access) (2, 3)));
  28. -- (2+3) + (2*3) = 11
  29. Ada.Text_IO.Put_Line (Integer'Image (Second_Order (Plus'Access, Mult'Access) (7, 5)));
  30. -- (7+5) + (7*5) = 47
  31. end Main;
stdin
Standard input is empty
compilation info
gnatgcc -c -pipe -O2 prog.adb
prog.adb:3:11: warning: file name does not match unit name, should be "main.adb"
gnatbind -x prog.ali
gnatlink prog.ali -o prog
stdout
 11
 47