#!/bin/runghc import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy.Char8 as L8 import Data.Char (isSpace) -- let's see if we can write a parser for this silly string sample = L8.pack "123 789" -- the results will go here data Duo = Duo { alpha :: Int, beta :: Int } instance Show Duo where show (Duo a b ) = "Duo " ++ show a ++ " and " ++ show b -- parse a number pInt :: L.ByteString -> Maybe (Int, L.ByteString) pInt s = case L8.readInt s of Nothing -> Nothing Just (num,rem) -> Just (num,rem) -- skip white space spaces :: L.ByteString -> Maybe (Int, L.ByteString) spaces s | isSpace $ L8.head s = Just (0, L8.dropWhile isSpace s) | otherwise = Nothing parse :: L.ByteString -> Maybe (Duo) parse arg = pInt arg >>= \(n1,s1) -> spaces s1 >>= \(_,s2) -> pInt s2 >>= \(n2,_) -> Just (Duo n1 n2) wrap :: L.ByteString -> String wrap arg = case parse arg of Nothing -> "trouble" Just x -> show x main = do let rv = wrap sample putStrLn rv -- THE END