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

Table of Contents

Problem Statement

In First-class functions, a language is showing how its manipulation of functions is similar to its manipulation of other types. This tasks aim is to compare and contrast a language's implementation of first class functions, with its normal handling of numbers.

Write a program to create an ordered collection of a mixture of literally typed and expressions producing a real number, together with another ordered collection of their multiplicative inverses. Try and use the following pseudo-code to generate the numbers for the ordered collections: Create a function multiplier, that given two numbers as arguments returns a function that when called with one argument, returns the result of multiplying the two arguments to the call to multiplier that created it and the argument in the call: Applying the multiplier of a number and its inverse from the two ordered collections of numbers in pairs, show that the result in each case is one. Compare and contrast the resultant program with the corresponding entry in First-class functions. They should be close. To paraphrase the task description: Do what was done before, but with numbers rather than functions

Let's start with the solution:

Step by Step solution about How to resolve the algorithm First-class functions/Use numbers analogously step by step in the BBC BASIC programming language

Source code in the bbc programming language

      REM Create some numeric variables:
      x = 2 : xi = 1/2
      y = 4 : yi = 0.25
      z = x + y : zi = 1 / (x + y)
      
      REM Create the collections (here structures are used):
      DIM c{x, y, z}
      DIM ci{x, y, z}
      c.x = x : c.y = y : c.z = z
      ci.x = xi : ci.y = yi : ci.z = zi
      
      REM Create some multiplier functions:
      multx = FNmultiplier(c.x, ci.x)
      multy = FNmultiplier(c.y, ci.y)
      multz = FNmultiplier(c.z, ci.z)
      
      REM Test applying the compositions:
      x = 1.234567 : PRINT x " ", FN(multx)(x)
      x = 2.345678 : PRINT x " ", FN(multy)(x)
      x = 3.456789 : PRINT x " ", FN(multz)(x)
      END
      
      DEF FNmultiplier(n1,n2)
      LOCAL f$, p%
      f$ = "(m)=" + STR$n1 + "*" + STR$n2 + "*m"
      DIM p% LEN(f$) + 4
      $(p%+4) = f$ : !p% = p%+4
      = p%


      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 Apply a callback to an array step by step in the MATLAB programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the PHP programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Tcl programming language
You may also check:How to resolve the algorithm Ultra useful primes step by step in the Java programming language
You may also check:How to resolve the algorithm Decorate-sort-undecorate idiom step by step in the FreeBASIC programming language