How to resolve the algorithm First-class functions step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm First-class functions step by step in the Delphi 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 Delphi programming language

Source code in the delphi programming language

program First_class_functions;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  System.Math;

type
  TFunctionTuple = record
    forward, backward: TFunc;
    procedure Assign(forward, backward: TFunc);
  end;

  TFunctionTuples = array of TFunctionTuple;

var
  cube, croot, fsin, fcos, faSin, faCos: TFunc;
  FunctionTuples: TFunctionTuples;
  ft: TFunctionTuple;

{ TFunctionTuple }

procedure TFunctionTuple.Assign(forward, backward: TFunc);
begin
  self.forward := forward;
  self.backward := backward;
end;

begin
  cube :=
    function(x: Double): Double
    begin
      result := x * x * x;
    end;

  croot :=
    function(x: Double): Double
    begin
      result := Power(x, 1 / 3.0);
    end;

  fsin :=
    function(x: Double): Double
    begin
      result := Sin(x);
    end;

  fcos :=
    function(x: Double): Double
    begin
      result := Cos(x);
    end;

  faSin :=
    function(x: Double): Double
    begin
      result := ArcSin(x);
    end;

  faCos :=
    function(x: Double): Double
    begin
      result := ArcCos(x);
    end;

  SetLength(FunctionTuples, 3);
  FunctionTuples[0].Assign(fsin, faSin);
  FunctionTuples[1].Assign(fcos, faCos);
  FunctionTuples[2].Assign(cube, croot);

  for ft in FunctionTuples do
    Writeln(ft.backward(ft.forward(0.5)):2:2);

  readln;
end.


  

You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Perl programming language
You may also check:How to resolve the algorithm Function definition step by step in the Lang programming language
You may also check:How to resolve the algorithm ABC problem step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the Racket programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Common Lisp programming language