Finding the length of a string

Haskell:
Finding the length of a string

How to:

-- Using the `length` function
main = do
    let myString = "Hello, Haskell!"
    print $ length myString

Sample Output:

15

Deep Dive

Haskell is a purely functional language where strings are represented as lists of characters. The length function, part of the Prelude (the default library imported into every Haskell program), operates under this representation.

Historically, strings as lists were a natural choice for Haskell due to their simplicity and the fact Lisp made a similar design choice (and influenced many functional languages). The length function just counts the elements in this list.

However, length is O(n), meaning the function will take time proportional to the length of the string. This isn’t an issue for short strings, but for long ones, it can be inefficient.

Alternatives include:

  • Using Text from the text package, a more efficient structure for Unicode text.
  • Utilizing ByteString from the bytestring package for binary or ASCII data.

Both offer a length function optimized for their respective data structures.

Implementation wise, a basic version of the length function could look like this:

myLength :: [a] -> Int
myLength [] = 0          -- The length of an empty list is 0
myLength (_:xs) = 1 + myLength xs  -- Recursively add 1 for the rest of the list

For Text and ByteString data types, they have their own internal implementation details that make them more efficient than a simple linked list of characters.

See Also