Updated May 31, 2023
Introduction to Haskell String
Haskell string is a data type which is used to store the value of variable in the form of string, string is represented by the sequence of character in Haskell or in any other programming language as well. String in Haskell provide different functions to manipulate the value of string object, or to perform any operation on it. In Haskell it is also very easiest form of data type which can be used, easy to handle and create as well. String can be created by using double quotes (“”) inside this we can write our string or value that we want string type to be hold for us.
We will have some closer look at its syntax for better usage while programming.
varibaleName :: String_type varibaleName = "some value here"
As you can see in the above lines of syntax it is very easy to use, also we can directly assign value to the variable using string literal.
Let’s take an practice syntax for better clarity of the program:
abc :: String_type abc = "my string"
By the above lines of code now we can understand how to define a string in Haskell.
How String Type Works in Haskell?
As now we already know that String is a type in Haskell which is used to store the sequence of the character in Haskell, it is very easy to create and handle. We can create string using string literal in Haskell. Like other programming language we are using “” double quotes to represent our string, if we do not surround our string by using the proper representation it will give us error. We have different function which can be used with string type in Haskell.
Let’s see the definition and usage of those functions:
This function is used to convert the string into upper case, if you pass the string in lower it will accordingly convert into upper case. This function return us string always, if the passing string is already in upper case then it will return the same result.
In the above mentioned ways we can use it.
2. to Lower
This function is used to convert the string into lower case, if you pass the string in upper it will accordingly convert into lower case. This function return us string always, if the passing string is already in lower case then it will return the same result.
In the above mentioned ways we can use it.
This function is used to break the string, separated by the white space while defining it. This function will always return us array of string contain the output.
word “yoour string here ..”
This function is the opposite of word function of string in Haskell, this will give us the single string which is being formed by the array of the elements. We will always have the string object as the return value from this function.
unwords “your array goes here /..”
This function also used to break the string but at the newline character. Here if we use ‘\n’ then it will break the string from there and create an array containing the result of the string. This function also return us the array as result.
lines “your string separated by newline character.”
This function does opposite of lines function in Haskell, it will join the string and return us the string object. Return type for this function is string value after joining. Also it will append \n or we can say a newline character after each string as result.
unlines “your array will go here ”
Given below are the examples of Haskell String:
In this example we are trying to convert the string type to upper case using the string function toUpper case from Haskell.
import Data.Char democovertupperacse :: String -> String democovertupperacse = map toUpper main = do print("Demo to show string type in Haskell !!") let result1 = democovertupperacse "hello" let result2 = democovertupperacse "I am lower case " let result3 = democovertupperacse " second string" let result4 = democovertupperacse "function of string" let result5 = democovertupperacse "testing" print("printing result of string ::") print("result one is :::") print(result1) print("result two is :::") print(result2) print("result three is :::") print(result3) print("result four is :::") print(result4) print("result five is :::") print(result5)
In this example we are trying to convert the string to array separated by the white spaces while creating, for this we are using words function from the string.
main = do print("Demo to show string type in Haskell !!") let val1 = "hello all world" let val2 = "i am string value" let val3 = "testing method" let val4 = "one two therre " let val5 = "100 200 300 400 500" let result1 = words val1 let result2 = words val2 let result3 = words val3 let result4 = words val4 let result5 = words val5 print("prinitng result ::") 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)
String is the most common and frequently sued type in Haskell or any other programming language because of its simplicity and ease of use. Also it is Ans in built type in Haskell, which comes up with so many different functions also, they can together be used to formed the string we want. Also very readable, easy, and handy to developers.
We hope that this EDUCBA information on “Haskell String” was beneficial to you. You can view EDUCBA’s recommended articles for more information.