Trusted answers to developer questions
Trusted Answers to Developer Questions

RELATED TAGS

haskell

How to find the length of a string in Haskell

Gutha Vamsi Krishna

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.

RELATED TAGS

haskell
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time

Copyright ©2022 Educative, Inc. All rights reserved.

soc2