- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Haskell program to add binary strings

This tutorial will discuss writing a program to add binary Strings in Haskell Programming Language. The computations in Haskell are Mathematical functions. Binary Strings represent a number in binary form but with String data type. Example: 5 is described as “0101”.

### Algorithmic steps

Take Binary Strings as input.

Implement the logic for adding binary Strings

Print the resultant Binary String.

Program to add Binary Strings

We break the program into simpler functions

### Syntax

Importing essential Packages/Modules

import Data.Char

Data.Char is a useful module for having functions to type cast Character to Integer and Integer to Character.

Function to Add Binary Strings

-- function declaration for addBString function addBStrings :: [Char]->[Char]->Int->[Char] -- function definitions for addBString function addBStrings [] [] carry = [intToDigit carry] addBStrings (x:xs) [] carry = intToDigit d : addBStrings xs [] newcarry where n1=digitToInt x sum1 = n1 + carry digit = mod sum1 2 carry1 = div sum1 2 d = digit newcarry = carry1 addBStrings [] (x:xs) carry = intToDigit d : addBStrings [] xs newcarry where n1=digitToInt x sum1 = n1 + carry digit = mod sum1 2 carry1 = div sum1 2 d = digit newcarry = carry1 addBStrings (x:xs) (y:ys) carry = intToDigit d : (addBStrings xs ys newcarry) where n1=digitToInt x n2=digitToInt y sum1 = n1 + n2 + carry digit = mod sum1 2 carry1 = div sum1 2 d = digit newcarry = carry1

In the above function,

We declared a function function addBStrings which takes two Strings (Character arrays) and Integer as an input and returns a String.

We defined a base case for the function when Strings are empty it returns carry as a list. We here converted the integer carry into a character using the function intToDigit which is available Data.Char module and we returned it as a list. In Haskell String is a list of characters.

In the Second definition, we defined a case where the second input String is Empty. In this case, First, we extracted the first character in the non-empty string. We then typecasted this character into an integer using the function digitToInt. We added the integer and carry. We returned the digit in the first place to be concatenated and we recursively called the addBstrings function with arguments as the remaining strings and new carry in addition.

Third Definition is similar to the Second which deals with a case if the First String is empty.

Fourth Definition works for all cases where argument strings are not empty, In this, we extract the first characters from each string, convert them to integers and add them to carry. Then we return the digit in one’s concatenated with a recursive call to the same function with remaining strings and new carry as parameters.

Helper function 1 for adding Binary Strings

-- function declaration helper :: [Char]->[Char]->[Char] -- function definition helper str1 str2 = reverse (addBStrings (reverse str1) (reverse str2) 0)

Above functions is an utility function used for reversing as the addBString function adds the digits from left to right, we need to reverse the strings, Before passing to that function. Function helper takes two strings to be added as input, reverses them, and passes them to the addBString function. This function returns the reverse of the result string to generate the correct output.

Helper function 2 for removing leading zeros

-- function declaration helper2 :: [Char]->[Char] -- function definition -- base condition helper2 [x] = [x] helper2 (x:xs) = if x == '0' then helper2 xs else (x:xs)

Above function removes the leading zeros in the output string returned by addBString function. This function takes a list/strings, extracts the first digit, and examines whether the digit is zero or not. If The character is zero it recursively calls itself with the remaining string until the result string doesn't have any leading zero i.e If the leading digit is not zero it calls the base condition which returns the remaining string. We invoke this function from the main with the result of the helper function 1 to remove the learning zeros.

### Overall program

**Example**

import Data.Char addBStrings :: [Char]->[Char]->Int->[Char] addBStrings [] [] carry = [intToDigit carry] addBStrings (x:xs) [] carry = intToDigit d : addBStrings xs [] newcarry where n1=digitToInt x sum1 = n1 + carry digit = mod sum1 2 carry1 = div sum1 2 d = digit newcarry = carry1 addBStrings [] (x:xs) carry = intToDigit d : addBStrings [] xs newcarry where n1=digitToInt x sum1 = n1 + carry digit = mod sum1 2 carry1 = div sum1 2 d = digit newcarry = carry1 addBStrings (x:xs) (y:ys) carry = intToDigit d : (addBStrings xs ys newcarry) where n1=digitToInt x n2=digitToInt y sum1 = n1 + n2 + carry digit = mod sum1 2 carry1 = div sum1 2 d = digit newcarry = carry1 helper :: [Char]->[Char]->[Char] helper str1 str2 = reverse (addBStrings (reverse str1) (reverse str2) 0) helper2 :: [Char]->[Char] -- base condition helper2 [x] = [x] helper2 (x:xs) = if x == '0' then helper2 xs else (x:xs) main :: IO () main = do let str1="11" let str2="101" print ("First Binary String is: " ++str1) print ("Second Binary String is: " ++str2) print ("Addition of the Binary Strings is: " ++ helper2 (helper str1 str2))

### Output

"First Binary String is: 11" "Second Binary String is: 101" "Addition of the Binary Strings is: 1000"

Above is theOverall Program, In main function, We declared and initialized variables for Strings and we called the function helper2 in which argument is a result of a function call for function helper which returns the resultant string for the addition of two strings. Now, this function takes the returned String and removes the leading zeros. Finally, We printed the string returned by function helper2.

## Conclusion

In this tutorial, we discussed how to write a program to add Binary Strings in Haskell Programming Language. We broke down the implementation into different functions to implement the logic.

- Related Articles
- Java Program to Add Two Binary Strings
- Program to add two binary strings in C++
- Add n binary strings?
- Program to add two binary strings, and return also as binary string in C++
- Add n binary strings in C++?
- How to add Two Binary Strings in Golang?
- Haskell Program to convert Binary to Octal
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- Haskell program to add two complex numbers
- Haskell Program to Check if two strings are anagram
- Haskell Program to compare numbers and strings using library function
- Haskell Program to convert the decimal number to binary using recursion
- Haskell program to convert a decimal number into a binary number
- Haskell Program to convert the Binary number to Gray code using recursion