## Introduction to Haskell tuple

Haskell tuple is the structure by which we can manage to store or group the values inside it. The tuple in Haskell is a fixed sized structure which can hold any type of data type inside it. We may have some requirements where we want to store different type of values inside the object, so to full fill this requirement we can use tuple in Haskell, which also provide several functions through which we can get the values from the tuple created. They are fixed in number so we can use them where we know the number of values need to be stored.

**Syntax:**

As we know that tuple is not a function rather it is a data structure to store the fixed number of data inside it.

Let’s see at its syntax to create tuple in Haskell.

`("value1 ", "value2 ", "value3 ", "value4 " // so on)`

As you can see in the above line of syntax we are just using the parentheses to relate the tuple in Haskell, inside this we can store any type of data we want.

**Example:**

**(100, “hello”)**

As you can see it is easy to create tuple in Haskell, and also easy to maintain.

4.8 (14,153 ratings)

View Course

### How tuple Function Works in Haskell?

As now we already know that tuples are used to store the values or group the values together, we also have list in Haskell but the difference between tuple and list is that, tuple can store different type of data inside it, but list cannot they should be of same type. But they both used to group the values together. Tuples are fixed in nature, so when we know the length or size we can use it while programming.

Here we will see more about the functions which are provided by tuple data structure in Haskell, also first we will see in detail how we can create tuple in Haskell.

#### 1. tuple

As we know that tuple are the in built functionality provided by Haskell, we do not require to include any external library or install any dependency. We can easily create it by using ‘()’ opening and closing parentheses in Haskell. Tuple can contain different type of data inside it like numbers, string boolean etc.

**tuple signature:**

`("String", Boolean, numbers)`

As you can see in the above line of code we are creating one tuple which contained string Boolean and number as the values inside it. But the creation process is very easy just we need to use small parentheses to create it in Haskell.

Now we will see some of the functions which can be used to tuples in Haskell so that we can perform operations on the tuple values.

Some of the functions are described as follows:

#### 1. fst

This tuple function is used to get the first element from the tuple values or group. We can use this function before the tuple and it will return us the first element as the result in Haskell.

**Syntax:**

`fst "your tuple"`

As you can see in the above lines of syntax we are trying to use fst function to get the first element from the tuple. So we have just used the word ‘fst’ just before the tuple either we can pass the tuple name or crate the tuple at this moment also.

#### 2. snd

This tuple function is used to get the second element from the tuple values or group. We can use this function before the tuple and it will return us the second element as the result in Haskell.

**Syntax:**

`snd "your tuple"`

As you can see in the above lines of syntax we are trying to use send function to get the second element from the tuple. So we have just used the word ‘snd’ just before the tuple either we can pass the tuple name or crate the tuple at this moment also.

#### 3. curry

This function tuple is used to convert the curried function to the uncurried one. As per the documentation available by Haskell for tuple.

**Syntax:**

`curry fst 100 200`

#### 4. uncurry

This function in tuple is used to convert the curried function to the sums or pair of functions in Haskell, as per the documentation of haskell.

**Syntax:**

`uncurry (+) (your tuple values)`

As you can see we are using uncurry function with the operator here which will add the pair of tuple and give us the sum as the result. We can use any operator here to perform the operations on the tuple.

### Example

Given below is the example mentioned:

In this example we are using fst function of tuple from Haskell.

**Code:**

`main = do`

print("Demo to show tuple functions in Haskell !!")

let tuple1 = (100, 200)

let tuple2 = (20.3, 450.5)

let tuple3 = ("hello", "world")

let tuple4 = ("i am first", "i am second")

let tuple5 = ("i am string", "i am second string")

let tuple6 = (260, 270)

print("calling methods of tuple in Haskell !!")

let result1 = fst tuple1

let result2 = fst tuple2

let result3 = fst tuple3

let result4 = fst tuple4

let result5 = fst tuple5

let result6 = fst tuple6

print("Printing result of function !!")

print("Result one is ", result1)

print("Result two is ", result2)

print("Result three is ", result3)

print("Result four is ", result4)

print("Result five is ", result5)

print("Result six is ", result6)

**Output:**

** **

### Conclusion

The tuples function allow us to modify our tuple element, they help us to get the data from the tuple as well. But tuples in Haskell give us some unction which can be used to manipulate the tuple values, there are no many functions available in Haskell for tuple. This are very easy to use, handle, readable by other developers also.

### Recommended Articles

This is a guide to Haskell tuple. Here we discuss the introduction, working along with the example for better understanding. You may also have a look at the following articles to learn more –