Haskell Program to convert the Binary number to Gray code using recursion


In Haskel we can use recursion along with helper function toconvert the Binary number to Gray code. In the first example, we are going to use base case, (grayCode "" = "" and grayCode [x] = [x]) and recursive case, grayCode (x:y:xs) = x : grayCode (xs ++ [if x == y then '0' else if x == '0' then '1' else '0'])). Where as in the second example, we are going to use two helper functions along with recursion.

Method 1: Converting the Binary number to Gray Code using recursion

In this method, the grayCode function is defined to handle three cases −

  • When the input string is empty, the function returns an empty string.

  • When the input string has only one character, the function returns the string unchanged.

  • When the input string has two or more characters, the function takes the first two characters, x and y, and uses them to calculate the next character in the Gray code. If x and y are equal, the next character is '0'. If x is '0', the next character is '1'. Otherwise, the next character is '0'. The function then recursively calls itself with the rest of the string (xs), and the result of this recursive call is appended to the current character..

Algorithm

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

grayCode "" = ""
grayCode [x] = [x]
grayCode (x:y:xs) = x : grayCode (xs ++ [if x == y then '0' else if x == '0' then '1' else '0']).
  • 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, a variable n is defined with the value "1101100111", and the result of grayCode n is printed to the console. The output will be the Gray code representation of the binary number n.

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

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

Example 1

In this example, we define a function grayCode that converts a binary number to its corresponding Gray code representation using recursion.

grayCode :: String -> String
grayCode "" = ""
grayCode [x] = [x]
grayCode (x:y:xs) = x : grayCode (xs ++ [if x == y then '0' else if x == '0' then '1' else '0'])

main :: IO ()
main = do
   let n = "1101100111"
   print (grayCode n)

Output

“1010100010”

Method 2: Converting the Binary number to Gray Code using recursion with the help of two helper function

In this method, the grayCode function handles the case when the input string has only one character, and returns the string unchanged. When the input string has two or more characters, the function takes the first character and passes it, along with the rest of the string, to the grayCodeR function. The grayCodeR function calculates the next character in the Gray code representation using the current character and the previous character. If the current character and the previous character are equal, the next character is '0'. If the current character is '0', the next character is '1'. Otherwise, the next character is '0'. The function then recursively calls itself with the rest of the string and the current character as the previous character.

Algorithm

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

grayCode "" = ""
grayCode [x] = [x]
grayCode (x:xs) = x : grayCodeR (xs, x).
  • Step 2 − The helper functions are defined as,

grayCodeR ("", prev) = ""
grayCodeR ([x], prev) = [if x == prev then '0' else '1']
grayCodeR (x:xs, prev) = (if x == prev then '0' else '1') : grayCodeR (xs, x).
  • 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. In the main function, a variable n is defined with the value "1101100111", and the result of grayCode n is printed to the console. The output will be the Gray code representation of the binary number n.

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

  • Step 5 − The resultant gray code number is printed to the console using ‘print’ function after the function is called.

Example 1

In this example, the recursive grayCode function uses two helper functions, grayCode and grayCodeR, to convert the binary number to its corresponding Gray code representation.

grayCode :: String -> String
grayCode "" = ""
grayCode [x] = [x]
grayCode (x:xs) = x : grayCodeR (xs, x)

grayCodeR :: (String, Char) -> String
grayCodeR ("", prev) = ""
grayCodeR ([x], prev) = [if x == prev then '0' else '1']
grayCodeR (x:xs, prev) = (if x == prev then '0' else '1') : grayCodeR (xs, x)

main :: IO ()
main = do
   let n = "1101100111"
   print (grayCode n)

Output

“1010100010”

Conclusion

In Haskell, the conversion of binary number to gray code number can be implemented using a recursive function that takes a binary number represented as a string, and returns its corresponding Gray code representation as a string. The function can perform the XOR operation on the bits and shift the binary number to the right, until all the bits have been processed.

Updated on: 27-Mar-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements