Haskell Program to convert Binary to Octal


In Haskell, we can use the user-defined function, Folder, reverse and helper function to convert binary to octal number. In the first example, we are going to use (binaryToOctal binary = showOct decimalValue "" where decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary) function. In the second example, we are going to use (binaryToOctal binary = showOct decimalValue "" where decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary octalDigits = reverse (octalHelper decimalValue) octalHelper n | n < 8 = [n] | otherwise = (n `mod` 8) : octalHelper (n `div` 8)).

Algorithm

  • Step 1 − The Data.Char and Numeric libraries are imported.

  • Step 2 − Define the binaryToOctal function

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − The variable named, “binaryNumber” is being initialized. It will hold the binary that is to be converted to respective octal number.

  • Step 5 − The resultant octal is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the function is defined using user-defined binaryToOctal function with the help of foldl function to convert the binary to octal number.

import Data.Char (digitToInt)
import Numeric (showOct)

binaryToOctal :: String -> String
binaryToOctal binary = showOct decimalValue ""
   where
      decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary

main :: IO ()
main = do
   let binaryNumber = "110101010"
   putStrLn ("Binary number: " ++ binaryNumber)
   putStrLn ("Octal number: " ++ binaryToOctal binaryNumber)

Output

Binary number: 110101010
Octal number: 652

Example 2

In this example, the function is defined using user-defined binaryToOctal function with the help of foldl and reverse function along with helper function to convert the binary to octal number.

import Data.Char (digitToInt)
import Numeric (showOct)

binaryToOctal :: String -> String
binaryToOctal binary = showOct decimalValue ""
   where
      decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary
      octalDigits = reverse (octalHelper decimalValue)
      octalHelper n
         | n < 8 = [n]
         | otherwise = (n `mod` 8) : octalHelper (n `div` 8)

main :: IO ()
main = do
   let binaryNumber = "110101010"
   putStrLn ("Binary number: " ++ binaryNumber)
   putStrLn ("Octal number: " ++ binaryToOctal binaryNumber)

Output

Binary number: 110101010
Octal number: 652

Conclusion

Binary to octal conversion is the process of converting a binary number (base 2) to its octal equivalent (base 8). To do this conversion, we need to group the binary digits into sets of three, starting from the rightmost digit, and then convert each set into its equivalent octal digit. In Haskell, a binary number is converted to octal using user-defined function along with foldl and reverse function. Also, we can use helper function with the user-defined function for conversion.

Updated on: 13-Mar-2023

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements