Haskell Program to convert the decimal number to binary using recursion


In Haskell, we will convert the decimal number to binary by using recursion and tail-recursion. In the first example, we are going to use base case, (decToBin 0 = "0" and decToBin 1 = "1") and recursive case, (decToBin n = decToBin (n `div` 2) ++ show (n `mod` 2)). Whereas in second example, we are going to use tail-recursive function.

Algorithm

  • Step 1 − The user defined, decToBin function is defined with base and recursive case as,

  • Example 1 and 2 −

decToBin 0 = "0"
decToBin 1 = "1"
decToBin n = decToBin (n `div` 2) ++ show (n `mod` 2).
  • Example 3 −

decToBin n = decToBin' n []
   where
      decToBin' 0 acc = acc
      decToBin' n acc = decToBin' (n `div` 2) (show (n `mod` 2) ++ acc).
  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, we define a variable n with the value 15, and print the result of decToBin n to the console. The output will be the binary representation of the decimal number 15.

  • Step 3 − The variable named, “n” is being initialized. It will hold the numbers which is to be converted to binary from decimal number.

  • Step 4 − The resultant binary number is printed to the console using ‘print’ function after the function is called.

Example 1

In this example, we define a function decToBin which takes an Int argument n and returns its binary representation as a String. The function uses recursion to convert the decimal number to binary.

decToBin :: Int -> String
decToBin 0 = "0"
decToBin 1 = "1"
decToBin n = decToBin (n `div` 2) ++ show (n `mod` 2)

main :: IO ()
main = do
   let n = 15
   print (decToBin n)

Output

“1111”

Example 2

In this example, the result is reversed using the reverse function, as the binary representation of the decimal number is built from the least significant bit to the most significant bit.

decToBin :: Int -> String
decToBin 0 = ""
decToBin n = decToBin (n `div` 2) ++ show (n `mod` 2)

main :: IO ()
main = do
   let n = 15
   print (reverse (decToBin n))

Output

“1111”

Example 3

In this example, we define a function tail-recursive decToBin which takes an Int argument n and returns its binary representation as a String. The function uses tail recursion to convert the decimal number to binary.

decToBin :: Int -> String
decToBin n = decToBin' n []
   where
      decToBin' 0 acc = acc
      decToBin' n acc = decToBin' (n `div` 2) (show (n `mod` 2) ++ acc)

main :: IO ()
main = do
   let n = 15
   print (decToBin n)

Output

“1111”

Conclusion

To convert a decimal number to binary in Haskell, one can write a function that implements the process of repeatedly dividing the decimal number by 2 and storing the remainder until the decimal number becomes 0. The binary representation of the decimal number is then constructed from the remainders obtained in the reverse order. The recursion is used to implement the same.

Updated on: 27-Mar-2023

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements