Haskell set | How set Works in Haskell | Example of Haskell set

Haskell set | How set Works in Haskell | Example of Haskell set

Haskell set | How set Works in Haskell | Example of Haskell set

Introduction to Haskell set

In Haskell as the name suggest set is used to store the elements, set are immutable in nature and comes under the data structure category, which provide us a way to store elements inside it, which should be in orders and unique as well. The set in Haskell provide us effective and efficient way of deletion, insertion and other operations. As we said they are immutable in nature that means if we try to modify the existing set it will always return us the new set containing the new modified elements.

Syntax of Haskell set

The set is used to store the elements, and also we have to use some package to use it inside our program.

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

import qualified Data.Set as Set:

name_of_set = Set.empty

name_of_set = Set.singleton 'your single value'

name_of_set = Set.fromList 'your list'

As you can see in the above line of syntax, we have three different approaches by the use of it we can create a set in Haskell, one issuing empty, another one using singleton and third is form the existing list we have

Let’s take an practice syntax:

demoset = Set.empty

As you can see in the above code of syntax we are trying to create an empty set by the use of ’empty’ method of set.

How set Works in Haskell?

The set is used to store the elements, set store the unique element, also in order in which they inserted into the set. We have one simplest way to create the set in Haskell by the use of existing list we have, also we can create it by using the empty and singleton method available in Haskell set.

Let’s take a closer look at the import packages which is required to import in the program:

Packs to use for set:

1. import qualified Data.Set as Set

This mentions package should be import in the program otherwise we will get error while creating the set in Haskell, because all the method of creating set present inside the package only. In order to access them it should be in the place.

2. Create set by using the existing list object

We can create set by the use of existing list variable. For this we can use the ‘fromList’ from the set package available. But to use this we have to have set package imported into the program.

Let’s have a look at the syntax for the ‘fromList’ to start using it.

name_of_set = Set.fromList 'your list'

As you can see in the above line of code, it is very easy to use a handle

Let’s take a sample piece of code:

Code:

demoset = Set.fromList [10, 20, 30]

This will create a set which contain the elements from the list, and it will return us the new set by the existing list object in Haskell.

3. Create an empty set

We can also create an empty set by the use of ’empty’ method available inside the set package, for this also set package is to be present in the program at the top.

Let’s take a look at the syntax for the ’empty’ to start using it while programming:

name_of_set = Set.empty

As you can see in the above line of code, it is very easy to use a handle.

Let’s take a sample piece of code:

Code:

demoset = Set.empty

This will create a set which is empty.

4. Create set using the single value

In Haskell we can also create a singleton set which will contain the single value inside it, for this we have ‘singleton’ method available in the set library of Haskell.

Let’s take an look at the syntax for the ‘singleton’ to start using it while programming:

name_of_set = Set.singleton 'your single value'

As you can see in the above line of code, it is very easy to use a handle.

let’s take an sample piece of code understand:

demoset = Set.singleton 'A'

In the following ways we can create set in Haskell, but the set package is important to include it, is also an in built function of Haskell, so we do not require to install any dependency for this to use in our program.

Example of Haskell set

Given below is the example mentioned:

In this example we are trying to create the set in Haskell using the set library and fromList function of the set package.

Code:

import qualified Data.Set as Set
set1 = Set.fromList ['a'..'z'] set2 = Set.fromList [10, 20, 30, 40, 50, 60] set3 = Set.fromList ["Hello", "world", "bye", "enjoy", "moment"] set4 = Set.fromList [1, 2, 3, 4, 5, 6, 7] set5 = Set.fromList [400, 200, 100, 500, 900, 800] set6 = Set.fromList ["value 1", "value 2", "value 3", "value 3", "value 4"] set7 = Set.fromList [1.1, 2.2, 3.3, 4.4, 5.5, 6.6] main = do
print("Demo to show set in Haskell !!")
print("Printing the result !!")
print("first set is :::", set1)
print("second set is :::", set2)
print("third is :::", set3)
print("fourth set is :::", set4)
print("fifth set is :::", set5)
print("sixth set is :::", set6)
print("seventh set is :::", set7)

Output:

Haskell set

Conclusion

By the use of set we can store our elements inside the set, it also main the orders of the insertion of the elements. One important thing about set that we only contained the unique elements, so in this way we can avoid duplicate data as well while preforming business logic for the application.

Recommended Articles

This is a guide to Haskell set. Here we discuss the introduction, how set works in Haskell? and example for better understanding. You may also have a look at the following articles to learn more –

  1. Haskell Where
  2. Haskell Programming
  3. JavaScript UUID
  4. JavaScript mouseover

The post Haskell set appeared first on EDUCBA.

Source link

By Manish Ransubhe

Founder OF Postintrend.com started my blogging journey in 2015 In This Duration I learned more about blogging, SEO, SEM, Affiliate marketing and more kinds of stuff on the Internet. I love to share my information with everyone.