好吧所以这里是我当前的代码:似乎无法实现正确要么
import System.IO
import System.Environment
import System.Directory
main = do
unfiltered <- getArgs ; home <- getHomeDirectory ; let db = home ++ "/.grindstone"
case unfiltered of
(x:xs) -> return()
_ -> error "No command given. See --help for more info."
command:args <- getArgs
createDirectoryIfMissing True db
let check = case args of
[] -> error "No arguments given. See --help for more info."
_ -> do let ([email protected](param:_),rest) = span (\(c:_) -> c=='-') args
if length params > 1 then error ("No arguments given for " ++ param)
else do
let (pArgs,_) = span (\(c:_) -> c/='-') rest
return (param, pArgs) :: Either (IO()) (String, [String])
let add = print "sup"
let cmds = [("add", add)]
let action = lookup command cmds
case action of
Nothing -> error "Unknown command."
(Just action) -> action
的主要问题是与检查。我试图实现任何一种类型,因为我希望它不是出错,或其他功能使用返回的东西,但是,它目前示数出有:
grindstone.hs:21:23:
No instance for (Monad (Either (IO())))
arising from a use of `return' at grindstone.hs:21:23-43
Possible fix:
add an instance declaration for (Monad (Either (IO())))
In the expression:
return (param, pArgs) :: Either (IO()) (String, [String])
In the expression:
do { let (pArgs, _) = span (\ (c : _) -> ...) rest;
return (param, pArgs) :: Either (IO()) (String, [String]) }
In the expression:
if length params > 1 then
error ("No arguments given for " ++ param)
else
do { let (pArgs, _) = ...;
return (param, pArgs) :: Either (IO()) (String, [String]) }
我只在Haskell和避风港开始了”对于monads来说,处理得太多了,所以我想我会在这里问。有人有主意吗?
HM ..谢谢。显然,当我在纯函数形式中定义checkArgs而不是在main中使用'Either(IO())(String,String)工作正常:P - http://paste.pocoo.org/show/344101/ – 2011-02-24 22:16:42