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

Source code in the picolisp programming language

(load "@lib/math.l")

(de multiplier (N1 N2)
   (curry (N1 N2) (X)
      (*/ N1 N2 X `(* 1.0 1.0)) ) )

(let (X 2.0  Xi 0.5  Y 4.0  Yi 0.25  Z (+ X Y)  Zi (*/ 1.0 1.0 Z))
   (mapc
      '((Num Inv)
         (prinl (format ((multiplier Inv Num) 0.5) *Scl)) )
      (list X Y Z)
      (list Xi Yi Zi) ) )

(load "@lib/math.l")

(de compose (F G)
   (curry (F G) (X)
      (F (G X)) ) )

(de cube (X)
   (pow X 3.0) )

(de cubeRoot (X)
   (pow X 0.3333333) )

(mapc
   '((Fun Inv)
      (prinl (format ((compose Inv Fun) 0.5) *Scl)) )
   '(sin  cos  cube)
   '(asin acos cubeRoot) )

  

You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Stack step by step in the Go programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Nim programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the Groovy programming language
You may also check:How to resolve the algorithm Empty string step by step in the Scheme programming language