Haskell Hacking: a journal of Haskell programming

Haskell Hacking has moved => Go here.
2006-10-04

Roll your own network code in Haskell

This week a medium length tutorial on network programming in Haskell. We build a concise IRC bot, and toss some custom control structures and a monad into the mixture. A small taste:

-- The 'Net' monad, a wrapper over IO, carrying the bot's immutable state.
type Net = ReaderT Bot IO
data Bot = Bot { socket :: Handle }

-- Set up actions to run on start and end, and run the main loop
main :: IO ()
main = bracket connect disconnect loop

listen :: Handle -> Net ()
listen h = forever $ do
    s <- io (hGetLine h)
    io (putStrLn s)
    when (ping s) $ pong s

Read the tutorial.

/home :: /haskell :: permalink :: rss

About

Real World Haskell

RWH Book Cover

Archives

Recommended

Blog Roll

Syndicate