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 = do
let str = "Hello from Educative!!!"
-- Get the string length
print(length str)

Explanation

  • Line 4: We initialize the str string.
  • Line 6: We use the length function to retrieve the length of the string and print it using the print statement.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources