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

Published on 12 May 2024 09:40 PM

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

Source code in the bbc programming language

      REM Create some functions and their inverses:
      DEF FNsin(a) = SIN(a)
      DEF FNasn(a) = ASN(a)
      DEF FNcos(a) = COS(a)
      DEF FNacs(a) = ACS(a)
      DEF FNcube(a) = a^3
      DEF FNroot(a) = a^(1/3)
      
      dummy = FNsin(1)
      
      REM Create the collections (here structures are used):
      DIM cA{Sin%, Cos%, Cube%}
      DIM cB{Asn%, Acs%, Root%}
      cA.Sin% = ^FNsin() : cA.Cos% = ^FNcos() : cA.Cube% = ^FNcube()
      cB.Asn% = ^FNasn() : cB.Acs% = ^FNacs() : cB.Root% = ^FNroot()
      
      REM Create some function compositions:
      AsnSin% = FNcompose(cB.Asn%, cA.Sin%)
      AcsCos% = FNcompose(cB.Acs%, cA.Cos%)
      RootCube% = FNcompose(cB.Root%, cA.Cube%)
      
      REM Test applying the compositions:
      x = 1.234567 : PRINT x, FN(AsnSin%)(x)
      x = 2.345678 : PRINT x, FN(AcsCos%)(x)
      x = 3.456789 : PRINT x, FN(RootCube%)(x)
      END
      
      DEF FNcompose(f%,g%)
      LOCAL f$, p%
      f$ = "(x)=" + CHR$&A4 + "(&" + STR$~f% + ")(" + \
      \             CHR$&A4 + "(&" + STR$~g% + ")(x))"
      DIM p% LEN(f$) + 4
      $(p%+4) = f$ : !p% = p%+4
      = p%


  

You may also check:How to resolve the algorithm Modified random distribution step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the zkl programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the C# programming language
You may also check:How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the Factor programming language
You may also check:How to resolve the algorithm Logical operations step by step in the GAP programming language