How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Haskell programming language
How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Haskell 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 Haskell programming language
This Haskell code defines a function that multiplies two numbers and a third number. It then uses this function to print a table of values, where each row shows the product of two numbers and 0.5.
Here is a breakdown of the code:
-
The
multiplier
function takes three arguments: two numbersa
andb
, and a functionm
that takes a single argument. It returns a new function that multipliesa
andb
bym
. -
The
main
function defines three lists:numbers
,inverses
, andpairs
.- The
numbers
list contains the numbers 2.0, 4.0, and 6.0. - The
inverses
list contains the numbers 0.5, 0.25, and 1.0. - The
pairs
list contains the pairs of numbers fromnumbers
andinverses
.
- The
-
The
print_pair
function takes a pair of numbers as an argument and prints a formatted string to the console. The string shows the product of the two numbers and 0.5. -
The
mapM_
function applies theprint_pair
function to each pair of numbers in thepairs
list. -
The output of the program is a table of values, where each row shows the product of two numbers and 0.5. For example, the first row shows the product of 2.0 and 0.5, which is 1.0.
Source code in the haskell programming language
module Main
where
import Text.Printf
-- Pseudo code happens to be valid Haskell
x = 2.0
xi = 0.5
y = 4.0
yi = 0.25
z = x + y
zi = 1.0 / ( x + y )
-- Multiplier function
multiplier :: Double -> Double -> Double -> Double
multiplier a b = \m -> a * b * m
main :: IO ()
main = do
let
numbers = [x, y, z]
inverses = [xi, yi, zi]
pairs = zip numbers inverses
print_pair (number, inverse) =
let new_function = multiplier number inverse
in printf "%f * %f * 0.5 = %f\n" number inverse (new_function 0.5)
mapM_ print_pair pairs
You may also check:How to resolve the algorithm Search a list step by step in the Prolog programming language
You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Prolog programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Arturo programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Run BASIC programming language