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

  1. multiplier is a proc that takes two numbers (n1 and n2) and returns a proc that takes a third number (m) and returns the product of n1, n2, and m.

  2. numlist is an array of three numbers: 2, 4, and 6.

  3. invlist is an array of three numbers: 0.5, 0.25, and 1.0/6.

  4. numlist.zip(invlist) zips the two arrays together, creating an array of three pairs:

  • [2, 0.5]
  • [4, 0.25]
  • [6, 1.0/6]
  1. .map {|n, invn| multiplier[invn, n][0.5]}:
  • n is the first element of the current pair and invn is the second element of the current pair.
  • This line passes invn and n as arguments to the multiplier proc.
  • The resulting proc takes 0.5 as an argument and returns 0.5.
  1. The result is an array of three numbers: [0.5, 0.5, 0.5].

Source code in the ruby programming language

multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
numlist = [x=2, y=4, x+y]
invlist = [0.5, 0.25, 1.0/(x+y)]
p numlist.zip(invlist).map {|n, invn| multiplier[invn, n][0.5]}
# => [0.5, 0.5, 0.5]


  

You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the REXX programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Raku programming language
You may also check:How to resolve the algorithm Time a function step by step in the C programming language
You may also check:How to resolve the algorithm Nested function step by step in the Scheme programming language