Below are some of the common Haskell libraries that I have used, along with their purposes and examples:
1. ‘text‘: Efficiently manipulate large Unicode text without using ’String’.
`usepackage{text}`
It provides better performance compared to the ’String’ type from the Haskell Prelude. This library is handy to perform various operations on text, like splitting, joining, etc.
Example usage:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
main :: IO ()
main = do
let original = "This is a Text example."
let splitted = T.splitOn " " original
putStrLn $ "Split: " ++ show splitted
2. ‘bytestring‘: Similar to ’text’, but for manipulating raw binary data.
`usepackage{bytestring}`
It allows you to efficiently manipulate ’ByteString’, an array of bytes. This library is heavily used when working with encoded strings, networking, serialization, etc.
Example usage:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Char8 as B
main :: IO ()
main = do
let original = B.pack "This is a ByteString example."
let modified = B.map toUpper original
putStrLn $ "Modified: " ++ B.unpack modified
3. ‘containers‘: A collection of efficient data structures, like ’Map’, ’Set’, ’Sequence’, etc.
`usepackage{containers}`
It provides efficient implementations of various standard data structures like ’Map’, ’Set’, and ’Sequence’ which are widely used in various applications.
Example usage:
import qualified Data.Map as M
main :: IO ()
main = do
let m = M.fromList [("one", 1), ("two", 2), ("three", 3)]
putStrLn $ "Value for key 'two': " ++ show (M.lookup "two" m)
4. ‘aeson‘: JSON parsing and encoding library.
`usepackage{aeson}`
This library is commonly used to parse and encode JSON data in Haskell applications.
Example usage:
{-# LANGUAGE DeriveGeneric #-}
import Data.Aeson (encode, decode)
import Data.ByteString.Lazy.Char8 (unpack)
import GHC.Generics
data Person = Person { name :: String, age :: Int } deriving (Show, Generic)
instance ToJSON Person
instance FromJSON Person
main :: IO ()
main = do
let jsonString = "{ "name": "John", "age": 30 }"
let decoded = decode jsonString :: Maybe Person
putStrLn $ "Decoded JSON: " ++ show decoded
5. ‘http-client‘: A library to make HTTP requests.
`usepackage{http-client}`
It allows you to make HTTP requests and work with the response in a relatively straightforward manner in Haskell.
Example usage:
import Network.HTTP.Client
import Network.HTTP.Types.Status (statusCode)
main :: IO ()
main = do
manager <- newManager defaultManagerSettings
request <- parseRequest "http://httpbin.org/get"
response <- httpLbs request manager
putStrLn $ "Status code: " ++ show (statusCode $ responseStatus response)
putStrLn $ "Response body: " ++ show (responseBody response)
These are just a few examples of the many commonly used libraries in the Haskell ecosystem. There are numerous other libraries that cater to specific needs, like ’lens’ for functional lenses or ’QuickCheck’ for property-based testing.