How to resolve the algorithm Partial function application step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Partial function application step by step in the Ada programming language

Table of Contents

Problem Statement

Partial function application   is the ability to take a function of many parameters and apply arguments to some of the parameters to create a new function that needs only the application of the remaining arguments to produce the equivalent of applying all arguments to the original function. E.g:

Note that in the partial application of a parameter, (in the above case param1), other parameters are not explicitly mentioned. This is a recurring feature of partial function application.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Partial function application step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO;

procedure Partial_Function_Application is

   type Sequence is array(Positive range <>) of Integer;

   -- declare a function FS with a generic parameter F and a normal parameter S
   generic
      with function F(I: Integer) return Integer; -- generic parameter
   function FS (S: Sequence) return Sequence;

   -- define FS
   function FS (S: Sequence) return Sequence is
      Result: Sequence(S'First .. S'Last);
   begin
      for Idx in S'Range loop
         Result(Idx) := F(S(Idx));
      end loop;
      return Result;
   end FS;

   -- define functions F1 and F2
   function F1(I: Integer) return Integer is
   begin
      return 2*I;
   end F1;

   function F2(I: Integer) return Integer is
   begin
      return I**2;
   end F2;

   -- instantiate the function FS by F1 and F2 (partially apply F1 and F2 to FS)
   function FSF1 is new FS(F1);
   function FSF2 is new FS(F2);

   procedure Print(S: Sequence) is
   begin
      for Idx in S'Range loop
         Ada.Text_IO.Put(Integer'Image(S(Idx)));
      end loop;
      Ada.Text_IO.New_Line;
   end Print;

begin
   Print(FSF1((0,1,2,3)));
   Print(FSF2((0,1,2,3)));
   Print(FSF1((2,4,6,8)));
   Print(FSF2((2,4,6,8)));
end Partial_Function_Application;


  

You may also check:How to resolve the algorithm Integer sequence step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Map range step by step in the Java programming language
You may also check:How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm A+B step by step in the Robotic programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Raku programming language