How to resolve the algorithm Balanced ternary step by step in the Haskell programming language
How to resolve the algorithm Balanced ternary step by step in the Haskell programming language
Table of Contents
Problem Statement
Balanced ternary is a way of representing numbers. Unlike the prevailing binary representation, a balanced ternary integer is in base 3, and each digit can have the values 1, 0, or −1.
Decimal 11 = 32 + 31 − 30, thus it can be written as "++−" Decimal 6 = 32 − 31 + 0 × 30, thus it can be written as "+−0"
Implement balanced ternary representation of integers with the following:
Test case With balanced ternaries a from string "+-0++0+", b from native integer -436, c "+-++-":
Note: The pages generalised floating point addition and generalised floating point multiplication have code implementing arbitrary precision floating point balanced ternary.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Balanced ternary step by step in the Haskell programming language
In Haskell programming language, the provided code focuses on the implementation of balanced ternary numbers, a particular type of numeric representation. Here's a detailed explanation:
-
Data Structure:
data BalancedTernary = Bt [Int]
: This is the data type for balanced ternary numbers, which are represented as lists of integers ([Int]
).
-
List Manipulation Functions:
zeroTrim
: This function removes leading zeros from a list of integers. It takes a list as an argument and returns a trimmed list.listBt
: This function converts a list of integers to a balanced ternary number (Bt [Int]
).
-
Normalization Function:
btNormalize
: This function normalizes a balanced ternary number by carrying over digits that exceed the ternary base (3). It converts a list of integers to a normalized balanced ternary number.
-
Conversion Functions:
btList
: This function extracts the list of integers from a balanced ternary number (Bt [Int]
).strBt
: This function converts a string of ternary digits ('-'
for -1,'0'
for 0, and'+'
for 1) to a balanced ternary number.intBt
: This function converts an integer to a balanced ternary number.btInt
: This function converts a balanced ternary number to an integer.
-
Arithmetic Operations:
Num
instance: The code definesNum
instance for balanced ternary numbers, providing implementations for arithmetic operations like addition ((+)
), multiplication ((*)
), and negation (negate
).
-
Signum Function:
signum
: This function returns the sign of a balanced ternary number. It returns0
forBt [0]
,Bt [1]
for positive numbers, andBt [-1]
for negative numbers.
-
Absolute Value Function:
abs
: This function returns the absolute value of a balanced ternary number. It returns the number itself if it's positive and the negation if it's negative.
-
Integer Conversion:
fromInteger
: This function converts an integer to a balanced ternary number.
-
Main Function:
main
: This function provides an example of using the defined functions. It creates three balanced ternary numbers (a
,b
,c
), multipliesa
byb - c
(r
), and prints the intermediate and final results.
Source code in the haskell programming language
data BalancedTernary = Bt [Int]
zeroTrim a = if null s then [0] else s where
s = fst $ foldl f ([],[]) a
f (x,y) 0 = (x, y++[0])
f (x,y) z = (x++y++[z], [])
btList (Bt a) = a
instance Eq BalancedTernary where
(==) a b = btList a == btList b
btNormalize = listBt . _carry 0 where
_carry c [] = if c == 0 then [] else [c]
_carry c (a:as) = r:_carry cc as where
(cc, r) = f $ (a+c) `quotRem` 3 where
f (x, 2) = (x + 1, -1)
f (x, -2) = (x - 1, 1)
f x = x
listBt = Bt . zeroTrim
instance Show BalancedTernary where
show = reverse . map (\d->case d of -1->'-'; 0->'0'; 1->'+') . btList
strBt = Bt . zeroTrim.reverse.map (\c -> case c of '-' -> -1; '0' -> 0; '+' -> 1)
intBt :: Integral a => a -> BalancedTernary
intBt = fromIntegral . toInteger
btInt = foldr (\a z -> a + 3 * z) 0 . btList
listAdd a b = take (max (length a) (length b)) $ zipWith (+) (a++[0,0..]) (b++[0,0..])
-- mostly for operators, also small stuff to make GHC happy
instance Num BalancedTernary where
negate = Bt . map negate . btList
(+) x y = btNormalize $ listAdd (btList x) (btList y)
(*) x y = btNormalize $ mul_ (btList x) (btList y) where
mul_ _ [] = []
mul_ as b = foldr (\a z -> listAdd (map (a*) b) (0:z)) [] as
-- we don't need to define binary "-" by hand
signum (Bt a) = if a == [0] then 0 else Bt [last a]
abs x = if signum x == Bt [-1] then negate x else x
fromInteger = btNormalize . f where
f 0 = []
f x = fromInteger (rem x 3) : f (quot x 3)
main = let (a,b,c) = (strBt "+-0++0+", intBt (-436), strBt "+-++-")
r = a * (b - c)
in do
print $ map btInt [a,b,c]
print $ r
print $ btInt r
You may also check:How to resolve the algorithm LZW compression step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Topological sort step by step in the OCaml programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Elixir programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Factor programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Standard ML programming language