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

Published on 12 May 2024 09:40 PM

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

Source code in the dart programming language

import 'dart:math' as Math;
cube(x) => x*x*x;
cuberoot(x)  => Math.pow(x, 1/3);
compose(f,g) => ((x)=>f(g(x)));
main(){
  var functions = [Math.sin, Math.exp, cube];
  var inverses = [Math.asin, Math.log, cuberoot];
  for (int i = 0; i < 3; i++){
    print(compose(functions[i], inverses[i])(0.5));
  }
}


  

You may also check:How to resolve the algorithm CRC-32 step by step in the PL/I programming language
You may also check:How to resolve the algorithm Numerical and alphabetical suffixes step by step in the Go programming language
You may also check:How to resolve the algorithm Image noise step by step in the Ada programming language
You may also check:How to resolve the algorithm Base64 decode data step by step in the Pike programming language
You may also check:How to resolve the algorithm Dot product step by step in the FreeBASIC programming language