Updated April 18, 2023
Introduction to Haskell zipWith
As the name suggests zipWith function in Haskell is used to zip the elements pausing to the function. With the zipWith function, we can pass our vales, zipWith always followed by one operator it can be anything on the basis of which it will zip the value of the argument and produce the result for us. This function is easy to use we use the ‘zipWith’ keyword to use this while programming in Haskell, also we will see which operator we can pass with the zipWith function in order to zip the arguments of the different arrays or lists in Haskell. In the coming section of the tutorial, we will see more about the internal working of the zipWith function, along with the implementation, and also its usage while programming in Haskell.
Syntax
As we already know that zipWith is very easy to use, readable and we can zip any number of elements by using this function. Let’s take a look at its syntax how we can use this while programming in Haskell see below;
zipWith (operator to be passed) array1 array2
As you can see in the above lines of syntax we have just used zipWith keyword followed by the operator and the list of arguments we want to calculate. Let’s see one practice syntax for beginners for a better understanding of the function for use see below;
Example:
zipWith (+) [val1] [val1]
From the above practice syntax, it is pretty much clear now, but in the coming section of the tutorial, we will see how we can use it, and internal working for better usage for beginners.
How zipWith function works in Haskell?
Now we already know that the zipWith function is used to zip the elements of passing input, this can be array or list in Haskell, also we have to pass the operator which will decide on which factor we have to merge this argument list in order to produce the result object. Also, in this section first, we will see the signature of the method given by the Haskell documentation for use see below;
Method Signature:
Below is the method signature for the zipWith function given by Haskell documentation. As we can see it take two arguments as the input parameter here, and produces the result as an array. Also, we have to use the operator while calling the zipWith function in Haskell. Otherwise, it will give us an error because it does not know on which basis we have to generate the output.
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Operators: Now we will discuss the operators that can be used with zipWith function in Haskell, on the basis on it output will be generated, which are as follows;
1. (+): The first operator here is addition if we will use this operator with the zipWith function then it will sum the elements of the passing an array as argument and produce the resultant array for us.
2. (-): The second operator here is subtraction if we will use this operator with the zipWith function then it will minus the elements of passing an array as argument, and produce the resultant array for us.
3. (*): The third operator here is multiplication if we will use this operator with the zipWith function then it will multiply the elements of the passing an array as argument, and produce the resultant array for us.
4. (/): The fourth operator here is divination if we will use this operator with the zipWith function then it will divide the elements of the passing an array as argument, and produce the resultant array for us.
The type which can be used with zipWith function in Haskell:
We can use the ZipWith function with any type available in Haskell we will discuss the different type in details which are as follows see below;
1. Numbers: ZipWith function can easily be used with numbers type in Haskell, this will zip the two numbers as the sum of them and return us the result.
2. String: If you want to use the ZipWith function with string then we have to use a different operator for this because if we use the addition it will give us an error, we have to use ++ this operator in order to concatenate two string present in the passes argument.
3. ByteString: it can be used with byte String type in Haskell.
4. Sequence: It can also be used with the sequence which will be resting us the sequence of the sums as result.
Points to remember while using it;
1. It is an inbuilt feature of Haskell, we do not require to include or download any dependency for this.
2. It can always be used with the operator which are applied with the type of array.
3. It can be used with any type in Haskell with no restriction.
Examples
Here are the follwoing examples mention below
Example #1
In this example we are trying to add the number and zip them using the zipWith function in Haskell, This is a sample example for beginners.
Code:
main = do
print("Demo to show zipWith in Haskell !!!")
let array1 = [100, 200, 300, 400, 500, 600, 700]
let array2 = [1, 2, 3, 4, 5 ,6 ,7]
let array3 = [1.2, 3.4, 5.8, 4.9, 10.9, 45.3]
let array4 = [11.5, 12.4, 0.6, 5.9, 6.10, 8.2]
print("print result of the zipfunction in haskell !!")
let result1 = zipWith (+) array1 array2
let result2 = zipWith (+) array3 array4
print("result one is ::", result1)
print("result two is ::", result2)
Output:
Example #2
In this example we are trying to add the string values and zip them using zipWith function in Haskell, This is an sample example for beginners.
Code:
main = do
print("Demo to show zipWith in Haskell !!!")
let array1 = ["A", "B", "C", "D", "E", "F", "G", "H"]
let array2 = ["a", "b", "c", "d", "e", "f", "g", "h"]
let array3 = ["hello ", "world ", "happy ", "eat ", "healthy "]
let array4 = ["valu1, ", "valu2 ", "valu3", "value4", "value5"]
print("print result of the zipfunction in Haskell !!")
let result1 = zipWith (++) array1 array2
let result2 = zipWith (++) array3 array4
print("result one is ::", result1)
print("result two is ::", result2)
Output:
Conclusion
By using the ZipWith function in Haskell, we can easily Manage or zip the passing argument into a single array. Also by the use of this, we can perform different operations on it like, addition, subtraction, multiplication, etc. This function is very easy to sue and handle, also readable by other developers, and easy to manage.
Recommended Articles
This is a guide to Haskell zipWith. Here we discuss How zipWith function works in Haskell along with the examples and outputs. You may also have a look at the following articles to learn more –