Skip to content

Instantly share code, notes, and snippets.

@JacobLeach
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobLeach/9816831 to your computer and use it in GitHub Desktop.
Save JacobLeach/9816831 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Char8
import Data.Word
import Control.Applicative
data IP = IP Int Int Int Int deriving (Show)
parseIP :: Parser IP
parseIP = do
d1 <- (check decimal)
char '.'
d2 <- (check decimal)
char '.'
d3 <- (check decimal)
char '.'
d4 <- (check decimal)
endOfInput
return (IP d1 d2 d3 d4)
check :: (Num a, Ord a) => Parser a -> Parser a
check p = p >>= check1
check1 :: (Num a, Ord a) => a -> Parser a
check1 n
| n < 256 = return (n)
| otherwise = fail "what"
main :: IO ()
main = print $ parseOnly parseIP "256.123.123.123"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment