
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Set of String to Array of String in Haskell
In Haskell, We can use listArray and toList functions as well as list comprehension to convert set of string to array of string. In the first example, we are going to use (setToArray set = listArray (1, Set.size set) (Set.toList set)) function and in the second example, we are going to call (setToArray set = listArray (1, Set.size set) (toList set)) function directly from the main function. And in the third example, we are going to use (setToArray set = array (1, Set.size set) [(i, x) | (i, x) <- zip [1..] (Set.toList set)]) function.
Algorithm
Step 1 ? The qualified Data.Set and Data.Array modules are imported.
Step 2 ? The setToArray function is defined using listArray function
Step 3 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 4 ? The variable named, ?set' is defined that will hold the Set of Strings that is to be converted to set of arrays.
Step 5 ? The resultant set of arrays corresponding to the set of strings is printed to the console, once the function is being called.
Example 1
In this example, Set of strings is converted to array of strings using listArray function.
import qualified Data.Set as Set import Data.Array setToArray :: Set.Set String -> Array Int String setToArray set = listArray (1, Set.size set) (Set.toList set) main :: IO () main = do let set = Set.fromList ["apple", "banana", "cherry", "banana", "apple"] array = setToArray set putStrLn $ "Set: " ++ show (Set.toList set) putStrLn $ "Array: " ++ show (elems array)
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... Set: ["apple","banana","cherry"] Array: ["apple","banana","cherry"]
Example 2
In this example, Set of strings is converted to array of strings using listArray and toList function.
import qualified Data.Set as Set import Data.Foldable (toList) import Data.Array (Array, listArray) setToArray :: Set.Set String -> Array Int String setToArray set = listArray (1, Set.size set) (toList set) main :: IO () main = do let mySet = Set.fromList ["apple", "banana", "orange"] myArray = setToArray mySet print myArray
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... array (1,3) [(1,"apple"),(2,"banana"),(3,"orange")]
Example 3
In this example, Set of strings is converted to array of strings using list comprehension.
import qualified Data.Set as Set import Data.Array setToArray :: Set.Set String -> Array Int String setToArray set = array (1, Set.size set) [(i, x) | (i, x) <- zip [1..] (Set.toList set)] main :: IO () main = do let mySet = Set.fromList ["apple", "banana", "orange"] myArray = setToArray mySet print myArray
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... array (1,3) [(1,"apple"),(2,"banana"),(3,"orange")]
Conclusion
In Haskell, a Set is a data structure that represents an unordered collection of distinct values. A Set of strings is a Set that contains only string values. An Array is another data structure that represents an ordered collection of values. An Array of strings is an Array that contains only string values. In Haskell, there are several ways to convert a Set of strings to an Array of strings, including using the Data.Set and Data.Array modules, list comprehension, and other libraries like vector.