[Haskell-beginners] Print values stored in an "Either" in the main method

David McBride toad3k at gmail.com
Fri Feb 21 01:01:33 UTC 2014


Because you didn't put all your code, I'm not sure if this is correct, but
my best guess is that you expect runEval to return Either String a, but the
way you are using it I suspect you actually want its type to be IO (Either
String a).

Alternatively change

result <- runEval env (eval (Plus (Var "x") (Lit 2)))

to

let result = runEval env (eval (Plus (Var "x") (Lit 2)))


On Thu, Feb 20, 2014 at 7:19 PM, Mathew Phillips
<mathewrphillips at gmail.com>wrote:

> I have a module that looks like this
>
> runEval :: (Show a) => Env -> Eval a -> Either String a
> runEval = ...
>
> data Value = IntVal Integer
>
> main :: IO ()
> main = do
>     let env = Map.fromList [("x",IntVal 3)]
>     result <- runEval env (eval (Plus (Var "x") (Lit 2)))
>     case result of
>         Left err -> putStrLn "Error: "++err
>         Right (IntVal i) -> print i
>
> when I call runEval from ghci I get back a result just fine
>
> let env = Map.fromList [("x",IntVal 3)]
> runEval env (eval (Plus (Var "x") (Lit 2)))
>
> This outputs "Right (IntVal 5)" as I would expect. But when I try and
> compile my main method I get the following error.
>
> Couldn't match expected type `Value'
> with actual type `Either t1 Value'
>     In the pattern: Right (IntVal i)
>     In a case alternative: Right (IntVal i) -> print i
>     In a stmt of a 'do' block:
>       case result of {
>         Left err -> print err
>         Right (IntVal i) -> print i }
>
> I also tried using (putStrLn . either show show) result, but this gave me
> the following error.
>
> Couldn't match expected type `Either a0 b0'
> with actual type `Value'
>     In the first argument of `putStrLn . either show show', namely
>       `result'
>     In a stmt of a 'do' block: (putStrLn . either show show) result
>     In the expression:
>       do { let env = Map.fromList ...;
>            result <- runEval3 env (eval3 (Plus (Var "x") (Lit 2)));
>            (putStrLn . either show show) result }
>
> Why is it when I try and pattern match on Either String Value it says
> result is of type "Value", but when I try and use either it says result is
> of type "Either String Value"?
>
> Matt P.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140220/e3379752/attachment.html>


More information about the Beginners mailing list