Haskell Program to Convert String to an Array


In Haskell, we will convert String to an Array by using listArray and intersperse functions. In the first example, we are going to use (stringToArray s = listArray (0, length s - 1) s) function and in the second example, we are going to use (stringToArray s = s) function. In the third example, we are going to use (stringToArray s = intersperse ' ' s).

Algorithm

  • Step 1 − The stringToArray function is defined

  • Step 2 − 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 3 − The variable, ‘myString’ is defined that will hold the String Value.

  • Step 4 − The resultant array value corresponding to the String value is printed to the console, once the function is being called.

Example 1

In this example, String is converted to Array using listArray function.

import Data.Array

stringToArray :: String -> Array Int Char
stringToArray s = listArray (0, length s - 1) s

main :: IO ()
main = do
  let myString = "hello"
  let myArray = stringToArray myString
  print myArray

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
array (0,4) [(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]

Example 2

In this example, String is converted to Array using list comprehension.

import Data.Array

stringToArray :: String -> Array Int Char
stringToArray s = array (0, length s - 1) [(i, s !! i) | i <- [0..length s - 1]]

main :: IO ()
main = do
  let myString = "hello"
  let myArray = stringToArray myString
  print myArray

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
array (0,4) [(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]

Example 3

In this example, String is converted to Array using user-defined function.

stringToArray :: String -> [Char]
stringToArray s = s

main :: IO ()
main = do
  let myString = "hello"
  let myArray = stringToArray myString
  print myArray

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
"hello"

Example 4

In this example, String is converted to Array using intersperse function.

import Data.List

stringToArray :: String -> [Char]
stringToArray s = intersperse ' ' s

main :: IO ()
main = do
  let myString = "hello"
  let myArray = stringToArray myString
  print myArray

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
"h e l l o"

Conclusion

In Haskell, a string is simply a list of characters. However, if we want to represent a string as an array of characters, we can use the Data.Array module, which provides functions for working with arrays. To convert a string to an array of characters, we can simply create a list of characters from the string, and then use the listArray and intersperse function to create an array from the list of strings.

Updated on: 28-Mar-2023

672 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements