What is the words function in Haskell?

Overview

The words function in Haskell is a built-in function that takes a String and separates it by whitespace characters.

Syntax

words :: String -> [String]

Parameters

The words function takes a String input as a parameter.

Return value

The words function returns a list of type [String]. Each element in the list is a String.

Code

Let’s look at the code below:

wordsList = "One\nTwo\tThree Four Five"
w = words wordsList
main = print w

Explanation

In the above code, we provide a String where words are separated by tabs, spaces, and newline characters.

The words function returns each word as a list and removes the whitespaces between them.

Free Resources