haskell - Can't seem to implement Either correctly -


alright here's current code:

import system.io import system.environment import system.directory  main =   unfiltered <- getargs ; home <- gethomedirectory ; let db = home ++ "/.grindstone"    case unfiltered of     (x:xs) -> return ()     _      -> error "no command given. see --help more info."   command:args <- getargs    createdirectoryifmissing true db    let check = case args of               [] -> error "no arguments given. see --help more info."               _  -> let (params@(param:_),rest) = span (\(c:_) -> c=='-') args                        if length params > 1 error ("no arguments given " ++ param)                          else                        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 

the main problem check. tried implementing either type since want either error out, or return function use, but, it's erroring out with:

grindstone.hs:21:23:     no instance (monad (either (io ())))       arising use of `return' @ grindstone.hs:21:23-43     possible fix:       add instance declaration (monad (either (io ())))     in expression:           return (param, pargs) :: either (io ()) (string, [string])     in expression:         { let (pargs, _) = span (\ (c : _) -> ...) rest;                return (param, pargs) :: either (io ()) (string, [string]) }     in expression:         if length params > 1             error ("no arguments given " ++ param)         else             { let (pargs, _) = ...;                    return (param, pargs) :: either (io ()) (string, [string]) } 

i'm starting out in haskell , haven't dealt monads yet thought i'd ask on here. have ideas?

the error causing compile problems directly casting expression type either (io ()) (string, [string]) when not either value. (the compiler not outputting helpful error message.)

to create either value [1], use data constructors left , right. convention (from library page) errors left value, while correct values right value.

i did quick rewrite of arg checking function

checkargs :: [string] -> either string (string, [string]) checkargs args =      case args of       [] -> left "no arguments given. see --help more info."       _  -> let (params@(param:_),rest) = span (\(c:_) -> c=='-') args in             if length params > 1                 left ("no arguments given " ++ param)             else                 let (pargs,_) = span (\(c:_) -> c/='-') rest in                right (param, pargs) 

note arg checking function not interact external io () library functions , has purely functional type. in general if code not have monadic elements (io ()), can clearer write in purely functional style. (when starting out in haskell recommend rather trying head around monads/monad transformers/etc immediately.)

when little more comfortable monads, may want check out control.monad.error [2], can wraps similar functionality either monad , encapsulate details left being computation errors.

[1] http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/data-either.html

[2] http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/control-monad-error.html


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -