How to resolve the algorithm First-class functions step by step in the Bori programming language
How to resolve the algorithm First-class functions step by step in the Bori 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 Bori programming language
Source code in the bori programming language
double acos (double d) { return Math.acos(d); }
double asin (double d) { return Math.asin(d); }
double cos (double d) { return Math.cos(d); }
double sin (double d) { return Math.sin(d); }
double croot (double d) { return Math.pow(d, 1/3); }
double cube (double x) { return x * x * x; }
Var compose (Var f, Var g, double x)
{
Func ff = f;
Func fg = g;
return ff(fg(x));
}
void button1_onClick (Widget widget)
{
Array arr1 = [ sin, cos, cube ];
Array arr2 = [ asin, acos, croot ];
str s;
for (int i = 1; i <= 3; i++)
{
s << compose(arr1.get(i), arr2.get(i), 0.5) << str.newline;
}
label1.setText(s);
}
0.5
0.4999999999999999
0.5000000000000001
You may also check:How to resolve the algorithm Knuth shuffle step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Mertens function step by step in the APL programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Colour bars/Display step by step in the Maple programming language
You may also check:How to resolve the algorithm De Polignac numbers step by step in the Phix programming language