How to resolve the algorithm Price fraction step by step in the Haskell programming language
How to resolve the algorithm Price fraction step by step in the Haskell programming language
Table of Contents
Problem Statement
A friend of mine runs a pharmacy. He has a specialized function in his Dispensary application which receives a decimal value of currency and replaces it to a standard value. This value is regulated by a government department.
Given a floating point value between 0.00 and 1.00, rescale according to the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Price fraction step by step in the Haskell programming language
The provided code snippet demonstrates two functions, price_fraction
, each taking a single argument n
. Both functions calculate a price fraction based on the value of n
.
-
The first version of the function uses pattern matching to handle different ranges of
n
values. It assigns specific price fractions for ranges liken < 0.06
,n < 0.11
,n < 0.16
, and so on, up ton < 1.00
. -
The second implementation uses a different approach with a
table
list of tuples. Each tuple in the table represents a range ofn
values, with the first element being the upper bound of the range, and the second element being the corresponding price fraction. The function uses thehead
function to find the first tuple where then
value is less than or equal to the upper bound, and returns the price fraction from that tuple.
Both versions of the function handle cases where n
is outside the valid range (less than 0 or greater than 1) by raising an error.
Source code in the haskell programming language
price_fraction n
| n < 0 || n > 1 = error "Values must be between 0 and 1."
| n < 0.06 = 0.10
| n < 0.11 = 0.18
| n < 0.16 = 0.26
| n < 0.21 = 0.32
| n < 0.26 = 0.38
| n < 0.31 = 0.44
| n < 0.36 = 0.50
| n < 0.41 = 0.54
| n < 0.46 = 0.58
| n < 0.51 = 0.62
| n < 0.56 = 0.66
| n < 0.61 = 0.70
| n < 0.66 = 0.74
| n < 0.71 = 0.78
| n < 0.76 = 0.82
| n < 0.81 = 0.86
| n < 0.86 = 0.90
| n < 0.91 = 0.94
| n < 0.96 = 0.98
| otherwise = 1.00
table = [
(0.06, 0.10), (0.11, 0.18), (0.16, 0.26), (0.21, 0.32), (0.26, 0.38),
(0.31, 0.44), (0.36, 0.50), (0.41, 0.54), (0.46, 0.58), (0.51, 0.62),
(0.56, 0.66), (0.61, 0.70), (0.66, 0.74), (0.71, 0.78), (0.76, 0.82),
(0.81, 0.86), (0.86, 0.90), (0.91, 0.94), (0.96, 0.98), (1.01, 1.00),
]
price_fraction n
| n < 0 || n > 1 = error "Values must be between 0 and 1."
| otherwise = snd $ head $ dropWhile ((<= n) . fst) table
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Oforth programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Oforth programming language
You may also check:How to resolve the algorithm Soloway's recurring rainfall step by step in the Python programming language
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the Julia programming language
You may also check:How to resolve the algorithm Create an object at a given address step by step in the Phix programming language