Check if string starts with other string in Haskell

Since strings are lists of characters, we can import Data.List and use the general function isPrefixOf:

isPrefixOf :: Eq a => [a] -> [a] -> Bool

Example:

Prelude Data.List> isPrefixOf "abc" "abcxyz"
True

with Data.Text

You can use isPrefixOf to check whether one string starts with another:

{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T

T.isPrefixOf "(" "(test string)"

To include Data.Text in your project, add text as a dependency in your Cabal file:

build-depends:
    base >=4.7 && <5
  , text

Tags:

Haskell