How to resolve the algorithm First-class functions step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

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

Source code in the pari/gp programming language

compose(f,g)={
  x -> f(g(x))
};

fcf()={
  my(A,B);
  A=[x->sin(x), x->cos(x), x->x^2];
  B=[x->asin(x), x->acos(x), x->sqrt(x)];
  for(i=1,#A,
    print(compose(A[i],B[i])(.5))
  )
};

  A=[sin, cos, x->x^2];
  B=[asin, acos, x->sqrt(x)];

  

You may also check:How to resolve the algorithm Greatest common divisor step by step in the AntLang programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Clean programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the TorqueScript programming language
You may also check:How to resolve the algorithm Metronome step by step in the PicoLisp programming language