Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import System.Environment
  2. import System.IO.Error
  3. data Parameters = Parameters String String [Int]
  4.  
  5. main :: IO ()
  6. main = getArgs
  7. >>= processArgs
  8. >>= either putStrLn doRealWork
  9.  
  10. processArgs :: [String] -> IO (Either String Parameters)
  11. processArgs args = (return $ enumerateArgs args)
  12. >>= (either (return . Left) parseArgs)
  13. -- This maybe could be improved, but it's not the focus
  14.  
  15. doRealWork :: Parameters -> IO ()
  16. doRealWork = undefined -- I'll implement the real work part later
  17.  
  18. enumerateArgs :: [String] -> Either String (String,String,String)
  19. enumerateArgs list
  20. | length list == 3 = Right (a,b,c)
  21. | otherwise = Left $ "Incorrect Argument Count,n"
  22. ++ "Expected 3 parametersn"
  23. ++ "Received: " ++ show list
  24. where (a:b:c:[]) = list
  25.  
  26. readFileEither :: String -> IO (Either String String)
  27. readFileEither = undefined -- it actually works, implementation is irrelevant
  28.  
  29. parseArgs' :: String -> String -> String -> Either String Parameters
  30. parseArgs' = undefined -- it actually works, implementation is irrelevant
  31.  
  32. parseArgs :: (String,String,String) -> IO (Either String Parameters)
  33. parseArgs (a,b,c) = readFileEither c >>= (x -> return . (x >>= (parseArgs' a b)))
  34. -- IO bind ^^^ Either bind ^^^
  35.  
  36. Couldn't match expected type `IO (Either String Parameters)'
  37. with actual type `a0 -> c0'
  38. In the expression: return . (x >>= (parseArgs' a b))
  39. In the second argument of `(>>=)', namely
  40. `( x -> return . (x >>= (parseArgs' a b)))'
  41. In the expression:
  42. readFileEither c >>= ( x -> return . (x >>= (parseArgs' a b)))
  43.  
  44. instance Monad (Either e) where
  45. return = Right
  46. Left l >>= _ = Left l
  47. Right r >>= k = k r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement