Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.Environment
- import System.IO.Error
- data Parameters = Parameters String String [Int]
- main :: IO ()
- main = getArgs
- >>= processArgs
- >>= either putStrLn doRealWork
- processArgs :: [String] -> IO (Either String Parameters)
- processArgs args = (return $ enumerateArgs args)
- >>= (either (return . Left) parseArgs)
- -- This maybe could be improved, but it's not the focus
- doRealWork :: Parameters -> IO ()
- doRealWork = undefined -- I'll implement the real work part later
- enumerateArgs :: [String] -> Either String (String,String,String)
- enumerateArgs list
- | length list == 3 = Right (a,b,c)
- | otherwise = Left $ "Incorrect Argument Count,n"
- ++ "Expected 3 parametersn"
- ++ "Received: " ++ show list
- where (a:b:c:[]) = list
- readFileEither :: String -> IO (Either String String)
- readFileEither = undefined -- it actually works, implementation is irrelevant
- parseArgs' :: String -> String -> String -> Either String Parameters
- parseArgs' = undefined -- it actually works, implementation is irrelevant
- parseArgs :: (String,String,String) -> IO (Either String Parameters)
- parseArgs (a,b,c) = readFileEither c >>= (x -> return . (x >>= (parseArgs' a b)))
- -- IO bind ^^^ Either bind ^^^
- Couldn't match expected type `IO (Either String Parameters)'
- with actual type `a0 -> c0'
- In the expression: return . (x >>= (parseArgs' a b))
- In the second argument of `(>>=)', namely
- `( x -> return . (x >>= (parseArgs' a b)))'
- In the expression:
- readFileEither c >>= ( x -> return . (x >>= (parseArgs' a b)))
- instance Monad (Either e) where
- return = Right
- Left l >>= _ = Left l
- Right r >>= k = k r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement