Simple Haskell calculator, using Maybe for error handling I'm reading through Write Yourself a Scheme after finishing going through Learn You a Haskell. I attempted one of the early exercises: writing a program to get an operator and two numbers and do a computation. It works fine.

Things I would like to know:

How should I structure a program, in terms of building larger functions out of smaller functions? Are there redundancies in my code?

What's the most effective way to use the Maybe type to indicate failure when main is of type IO ()? Is my checkSuccess an appropriate way to do this?

module Main where

import System.Environment

-- parses the first arithmetic operator in a string
parseOperator :: String -&gt; Maybe Char
parseOperator [] = Nothing
parseOperator (x:xs)
    | x == '*' = Just '*'
    | x == '/' = Just '/'
    | x == '+' = Just '+'
    | x == '-' = Just '-'
    | otherwise = parseOperator xs

parseNum :: String -&gt; Maybe Double
parseNum x =
    let parsed = reads x :: [(Double,String)]
    in case parsed of
        [(a,"")] -&gt; Just a
        [(_,_)] -&gt; Nothing
        [] -&gt; Nothing

compute :: Maybe Char -&gt; Maybe Double -&gt; Maybe Double -&gt; Maybe Double
compute Nothing _ _ = Nothing
compute _ Nothing _ = Nothing
compute _ _ Nothing = Nothing
compute (Just c) (Just x) (Just y)
    | c == '*' = Just $ x * y
    | c == '/' = Just $ x / y
    | c == '+' = Just $ x + y
    | c == '-' = Just $ x - y

checkSuccess :: Maybe Double -&gt; IO ()
checkSuccess Nothing = putStrLn "Failed. Check correctness of inputs"
checkSuccess (Just r) = putStrLn $ "Result: " ++ (show r)

runSequence :: String -&gt; String -&gt; String -&gt; IO ()
runSequence os xs ys =
    checkSuccess $ compute (parseOperator os) (parseNum xs) (parseNum ys)

main = do
    putStrLn "Enter operator: * / + -"
    operator &lt;- getLine

    putStrLn "Enter first number"
    first &lt;- getLine

    putStrLn "Enter second number"
    second &lt;- getLine

    runSequence operator first second
I'd further suggest to split compute into two parts:


Getting the right operation from a given character:

op :: Char -&gt; Maybe (Double -&gt; Double -&gt; Double)
op '*' = Just (*)
op '/' = Just (/)
op '+' = Just (+)
op '-' = Just (-)
op _   = Nothing


This accepts a character, and either produces a function corresponding to the character, or Nothing.
Applying the operation on values, taking into account all possible Maybes:

compute' :: Maybe Char -&gt; Maybe Double -&gt; Maybe Double -&gt; Maybe Double
compute' c x y = (c &gt;&gt;= op) &lt;*&gt; x &lt;*&gt; y


Here c &gt;&gt;= op results into Maybe (Double -&gt; Double -&gt; Double), where the result is Nothing if the character is Nothing or if it doesn't match any supported operation. And then we use twice &lt;*&gt; from Control.Applicative, which, specialized for Maybe, applies Maybe (a -&gt; b) to Maybe a yelding Maybe b.