CodeToLive

Common Haskell Libraries

Haskell has a rich ecosystem of libraries for various purposes. Here are some of the most commonly used ones across different domains.

Text Processing

Libraries for working with text data:


-- text: Efficient Unicode text processing
import qualified Data.Text as T
import Data.Text.IO as TIO

main :: IO ()
main = TIO.putStrLn (T.pack "Hello, Text!")

-- parsec: Parser combinators
import Text.Parsec
import Text.Parsec.String

numberParser :: Parser Int
numberParser = read <$> many1 digit

-- aeson: JSON processing
import Data.Aeson
import qualified Data.ByteString.Lazy as B

data Person = Person { name :: String, age :: Int }
instance FromJSON Person where
  parseJSON = withObject "Person" $ \v -> Person
    <$> v .: "name"
    <*> v .: "age"

decodePerson :: B.ByteString -> Maybe Person
decodePerson = decode
      

Concurrency and Parallelism

Libraries for concurrent and parallel programming:


-- async: Higher-level concurrency
import Control.Concurrent.Async

concurrentComputation :: IO ()
concurrentComputation = do
  (a, b) <- concurrently (return 1) (return 2)
  print (a + b)

-- stm: Software Transactional Memory
import Control.Concurrent.STM

transfer :: TVar Int -> TVar Int -> Int -> STM ()
transfer from to amount = do
  fromBal <- readTVar from
  when (fromBal < amount) retry
  writeTVar from (fromBal - amount)
  toBal <- readTVar to
  writeTVar to (toBal + amount)
      

Data Structures

Libraries providing efficient data structures:


-- containers: Common data structures
import qualified Data.Map as Map
import qualified Data.Set as Set

mapExample :: Map.Map String Int
mapExample = Map.fromList [("Alice", 25), ("Bob", 30)]

setExample :: Set.Set Int
setExample = Set.fromList [1, 2, 3, 4, 5]

-- vector: High-performance arrays
import qualified Data.Vector as V

vectorSum :: V.Vector Int -> Int
vectorSum = V.foldl' (+) 0
      

Web Development

Libraries for building web applications:


-- warp: Lightweight web server
import Network.Wai
import Network.Wai.Handler.Warp (run)

app :: Application
app _ respond = respond $ responseLBS status200 [] "Hello, World!"

main :: IO ()
main = run 8080 app

-- scotty: Web framework
import Web.Scotty

main :: IO ()
main = scotty 3000 $ do
  get "/" $ text "Hello, Scotty!"
  get "/hello/:name" $ do
    name <- param "name"
    text ("Hello, " ++ name ++ "!")

-- servant: Type-safe web APIs
import Servant
import Servant.API
import Servant.Server

type API = "users" :> Get '[JSON] [User]
      :<|> "users" :> Capture "id" Int :> Get '[JSON] User

server :: Server API
server = getUsers :<|> getUserById
      

Database Access

Libraries for working with databases:


-- persistent: Database abstraction
import Database.Persist
import Database.Persist.Sqlite

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
  name String
  age Int
  deriving Show
|]

main :: IO ()
main = runSqlite ":memory:" $ do
  runMigration migrateAll
  johnId <- insert $ Person "John Doe" 30
  john <- get johnId
  liftIO $ print john

-- postgresql-simple: PostgreSQL client
import Database.PostgreSQL.Simple

main :: IO ()
main = do
  conn <- connectPostgreSQL "dbname=test"
  [Only i] <- query_ conn "select 2 + 2"
  print i
      

Testing

Libraries for testing Haskell code:


-- hspec: Behavior-driven development
import Test.Hspec

main :: IO ()
main = hspec $ do
  describe "Addition" $ do
    it "1 + 1 = 2" $ do
      1 + 1 `shouldBe` 2

-- QuickCheck: Property-based testing
import Test.QuickCheck

prop_reverseReverse :: [Int] -> Bool
prop_reverseReverse xs = reverse (reverse xs) == xs

main :: IO ()
main = quickCheck prop_reverseReverse
      

Streaming and Conduits

Libraries for streaming data processing:


-- conduit: Streaming data library
import Data.Conduit
import qualified Data.Conduit.List as CL

main :: IO ()
main = runConduit $ 
  yieldMany [1..10] .| CL.map (*2) .| CL.mapM_ print

-- pipes: Compositional streams
import Pipes
import qualified Pipes.Prelude as P

main :: IO ()
main = runEffect $ 
  each [1..10] >-> P.map (*2) >-> P.print
      

Mathematics and Science

Libraries for mathematical and scientific computing:


-- ad: Automatic differentiation
import Numeric.AD

newton :: (Double -> Double) -> Double -> Double
newton f x0 = iterate go x0 !! 10
  where
    go x = x - f x / diff f x

-- hmatrix: Linear algebra
import Numeric.LinearAlgebra

main :: IO ()
main = do
  let a = (2 >< 2) [1, 2, 3, 4] :: Matrix Double
  print (inv a)
      

Finding Libraries

Resources for discovering Haskell libraries:

Dependency Management

Tools for managing Haskell dependencies:

← Previous: Concurrent Haskell