-->

Convert Binary to Decimal Haskell

2020-07-27 05:09发布

问题:

I need a program in which I enter a binary system number, and return that same number to me in decimal base.

binToDec :: Integer -> Integer

binToDec 1110101011111111000000111100001010101011110010000001 = 4134096010394753

binToDec 111111111111111111111111111111111111111111111111111111111111111 =  9223372036854775807 

I also need the examples above to be compiled in less than 5 seconds.

I have managed to do this, but the problem is that it starts from a list of integers:

binToDec l = sum $ map (2^) $ findIndices (==1) $ reverse l

回答1:

Given the "decimal" number only contains zeros and ones, we can use recursion for this:

bintodec :: Integral i => i -> i
bintodec 0 = 0
bintodec i = 2 * bintodec (div i 10) + (mod i 10)

So what we do here is using div i 10 to "shift the number one to the right". We use recursion and multiply with two to use binary representation. We also use mod i 10 to obtain the last digit that we then add to the number.

As said before this is not very safe, since for instance it will also produce a number for bintodec 10010202010, we can in that case return a Maybe i with:

bintodec :: Integral i => i -> Maybe i
bintodec 0 = Just 0
bintodec i | last < 2 = fmap (\x -> 2*x + last) (bintodec (div i 10))
           | otherwise = Nothing
    where last = mod i 10

This then produces:

Prelude> bintodec 1110101011111111000000111100001010101011110010000001
Just 4134096010394753
Prelude> bintodec 11101010111111110000001111000010101014011110010000001
Nothing

It is however better to use a [Bool] for instance, since this will enforce that we can not provide a value that is not a valid bitstring. In that case we can use:

bintodec :: [Bool] -> Int
bintodec = foldr (\x y -> fromEnum x + 2*y) 0