Haskell:
Extracting substrings

How to:

In Haskell, you can slice and dice strings with built-in functions like take, drop, and substring (from Data.Text).

import Data.Text (Text, pack, unpack, take, drop)

-- Our example string
let exampleStr = "Haskell makes sense!"

-- Taking the first 7 characters
print $ unpack (take 7 (pack exampleStr)) -- "Haskell"

-- Dropping the first 8 characters
print $ unpack (drop 8 (pack exampleStr)) -- "makes sense!"

-- Custom function to extract a substring by position and length
substring :: Int -> Int -> Text -> Text
substring start length = take length . drop start

-- Extract "makes" (starting from position 8, length 5)
print $ unpack (substring 8 5 (pack exampleStr)) -- "makes"

Sample output:

"Haskell"
"makes sense!"
"makes"

Deep Dive

Extracting substrings has been part of Haskell for ages. Early on, it relied on lists, since strings are lists of characters. Performance wasn’t great. Enter Data.Text, offering efficient string operations.

Alternatives include list operations, regex, and parsing libraries. List ops are simpler but slower for big strings. Regex is powerful but overkill for simple tasks. Parsing libraries are for complex parsing but can handle substrings too.

Implementing a custom substring function in Haskell is straightforward using take and drop from Data.Text, providing faster string handling than list-based operations.

See Also