Skip to content

Instantly share code, notes, and snippets.

@queertypes
Created June 13, 2014 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queertypes/7c6a28ad904a3b96f1c3 to your computer and use it in GitHub Desktop.
Save queertypes/7c6a28ad904a3b96f1c3 to your computer and use it in GitHub Desktop.
Parse an IP address with attoparsec
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Attoparsec.Text
import Data.Word
data IP = IP Word8 Word8 Word8 Word8 deriving (Eq, Show)
parseIP :: Parser IP
parseIP = IP <$> dec <*> dec <*> dec <*> decimal <?> "parse IP Address"
where dot = char '.'
dec = decimal <* dot
main :: IO ()
main = print $ parseOnly parseIP "1.2.3.4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment