How to resolve the algorithm Map range step by step in the Haskell programming language
How to resolve the algorithm Map range step by step in the Haskell programming language
Table of Contents
Problem Statement
Given two ranges: where:
Write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map values from the range [0, 10] to the range [-1, 0].
Show additional idiomatic ways of performing the mapping, using tools available to the language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Map range step by step in the Haskell programming language
The provided Haskell program demonstrates the process of mapping values from one range to another. It does this for both floating-point numbers and rational numbers.
The mapRange
function takes four parameters:
(a1, a2)
: The original range from which the value is being mapped.(b1, b2)
: The target range to which the value is being mapped.s
: The value to be mapped.
The mapRange
function uses linear interpolation to calculate the mapped value using the following formula:
b1 + (s - a1) * (b2 - b1) / (a2 - a1)
The main
function is the entry point of the program. It performs the following steps:
- It prints a header for the floating-point mapping.
- It uses
mapM_
to iterate over a range of integers from 0 to 10 and applies themapRange
function to each integer, with the mapping from(0, 10)
to(-1, 0)
and converting the result to a double-precision floating-point number. - It prints each mapped value using the
prtD
function, which formats the output as an integer followed by the mapped value with three decimal places. - It prints a header for the rational mapping.
- It performs the same mapping as before, but this time using rational numbers instead of floating-point numbers.
- It prints each mapped value using the
prtR
function, which formats the output as an integer followed by the mapped value as a string.
In summary, this program demonstrates the process of mapping values from one range to another, using both floating-point numbers and rational numbers, and shows the results in a formatted manner.
Source code in the haskell programming language
import Data.Ratio
import Text.Printf (PrintfType, printf)
-- Map a value from the range [a1,a2] to the range [b1,b2]. We don't check
-- for empty ranges.
mapRange
:: Fractional a
=> (a, a) -> (a, a) -> a -> a
mapRange (a1, a2) (b1, b2) s = b1 + (s - a1) * (b2 - b1) / (a2 - a1)
main :: IO ()
main
-- Perform the mapping over floating point numbers.
= do
putStrLn "---------- Floating point ----------"
mapM_ (\n -> prtD n . mapRange (0, 10) (-1, 0) $ fromIntegral n) [0 .. 10]
-- Perform the same mapping over exact rationals.
putStrLn "---------- Rationals ----------"
mapM_ (\n -> prtR n . mapRange (0, 10) (-1, 0) $ n % 1) [0 .. 10]
where
prtD
:: PrintfType r
=> Integer -> Double -> r
prtD = printf "%2d -> %6.3f\n"
prtR
:: PrintfType r
=> Integer -> Rational -> r
prtR n x = printf "%2d -> %s\n" n (show x)
You may also check:How to resolve the algorithm URL parser step by step in the Java programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the Ada programming language
You may also check:How to resolve the algorithm Nim game step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Common Lisp programming language