How to find the length of a string in Haskell
Overview
A string is a list of characters in Haskell. We can get the length of the string using the length function.
Syntax
length <string_name>
Parameters
The length function takes a list as a parameter. Since the string is a list of characters, we can pass the string as a parameter and get its length.
Return value
The length function returns the length of a string as an integer.
Example
Let's take a look at an example of this.
main::IO()main = dolet str = "Hello from Educative!!!"-- Get the string lengthprint(length str)
Explanation
- Line 4: We initialize the
strstring. - Line 6: We use the
lengthfunction to retrieve the length of the string and print it using theprintstatement.