How to resolve the algorithm First-class functions step by step in the Ada programming language
How to resolve the algorithm First-class functions step by step in the Ada programming language
Table of Contents
Problem Statement
A language has first-class functions if it can do each of the following without recursively invoking a compiler or interpreter or otherwise metaprogramming:
Write a program to create an ordered collection A of functions of a real number. At least one function should be built-in and at least one should be user-defined; try using the sine, cosine, and cubing functions. Fill another collection B with the inverse of each function in A. Implement function composition as in Functional Composition. Finally, demonstrate that the result of applying the composition of each function in A and its inverse in B to a value, is the original value. (Within the limits of computational accuracy). (A solution need not actually call the collections "A" and "B". These names are only used in the preceding paragraph for clarity.)
First-class Numbers
Let's start with the solution:
Step by Step solution about How to resolve the algorithm First-class functions step by step in the Ada programming language
Source code in the ada programming language
with Ada.Float_Text_IO,
Ada.Integer_Text_IO,
Ada.Text_IO,
Ada.Numerics.Elementary_Functions;
procedure First_Class_Functions is
use Ada.Float_Text_IO,
Ada.Integer_Text_IO,
Ada.Text_IO,
Ada.Numerics.Elementary_Functions;
function Sqr (X : Float) return Float is
begin
return X ** 2;
end Sqr;
type A_Function is access function (X : Float) return Float;
generic
F, G : A_Function;
function Compose (X : Float) return Float;
function Compose (X : Float) return Float is
begin
return F (G (X));
end Compose;
Functions : array (Positive range <>) of A_Function := (Sin'Access,
Cos'Access,
Sqr'Access);
Inverses : array (Positive range <>) of A_Function := (Arcsin'Access,
Arccos'Access,
Sqrt'Access);
begin
for I in Functions'Range loop
declare
function Identity is new Compose (Functions (I), Inverses (I));
Test_Value : Float := 0.5;
Result : Float;
begin
Result := Identity (Test_Value);
if Result = Test_Value then
Put ("Example ");
Put (I, Width => 0);
Put_Line (" is perfect for the given test value.");
else
Put ("Example ");
Put (I, Width => 0);
Put (" is off by");
Put (abs (Result - Test_Value));
Put_Line (" for the given test value.");
end if;
end;
end loop;
end First_Class_Functions;
You may also check:How to resolve the algorithm Hamming numbers step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the C++ programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Tupper's self-referential formula step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the Rust programming language