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

Source code in the common programming language

(defun multiplier (f g)
  #'(lambda (x) (* f g x)))

(let* ((x 2.0)
       (xi 0.5)
       (y 4.0)
       (yi 0.25)
       (z (+ x y))
       (zi (/ 1.0 (+ x y)))
       (numbers (list x y z))
       (inverses (list xi yi zi)))
  (loop with value = 0.5
        for number in numbers
        for inverse in inverses
        for multiplier = (multiplier number inverse)
        do (format t "~&(~A * ~A)(~A) = ~A~%"
                   number
                   inverse
                   value
                   (funcall multiplier value))))


(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
(defun cube (x) (expt x 3))
(defun cube-root (x) (expt x (/ 3)))

(loop with value = 0.5
      for function in (list #'sin  #'cos  #'cube     )
      for inverse  in (list #'asin #'acos #'cube-root)
      for composed = (compose inverse function)
      do (format t "~&(~A ∘ ~A)(~A) = ~A~%"
                 inverse
                 function
                 value 
                 (funcall composed value)))


  

You may also check:How to resolve the algorithm Terminal control/Hiding the cursor step by step in the Ada programming language
You may also check:How to resolve the algorithm Dining philosophers step by step in the Pike programming language
You may also check:How to resolve the algorithm String comparison step by step in the Wren programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Plain TeX programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the Raku programming language