title: Haskell: if-then-else parse error
body: <p>I'm working on an instance of Read ComplexInt.</p>
  
  <p>Here's what was given:</p>
  
  <pre><code>data ComplexInt = ComplexInt Int Int
  deriving (Show)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>module Parser (Parser,parser,runParser,satisfy,char,string,many,many1,(+++)) where
  
  import Data.Char
  import Control.Monad
  import Control.Monad.State
  
  type Parser = StateT String []
  
  runParser :: Parser a -&gt; String -&gt; [(a,String)]
  runParser = runStateT
  
  parser :: (String -&gt; [(a,String)]) -&gt; Parser a
  parser = StateT
  
  satisfy :: (Char -&gt; Bool) -&gt; Parser Char
  satisfy f = parser $ \s -&gt; case s of
      [] -&gt; []
      a:as -&gt; [(a,as) | f a]
  
  char :: Char -&gt; Parser Char
  char = satisfy . (==)
  
  alpha,digit :: Parser Char
  alpha = satisfy isAlpha
  digit = satisfy isDigit
  
  string :: String -&gt; Parser String
  string = mapM char
  
  infixr 5 +++
  (+++) :: Parser a -&gt; Parser a -&gt; Parser a
  (+++) = mplus
  
  many, many1 :: Parser a -&gt; Parser [a]
  many p = return [] +++ many1 p
  many1 p = liftM2 (:) p (many p)
  </code></pre>
  
  <p>Here's the given exercise:</p>
  
  <pre><code>"Use Parser to implement Read ComplexInt, where you can accept either the simple integer 
  syntax "12" for ComplexInt 12 0 or "(1,2)" for ComplexInt 1 2, and illustrate that read 
  works as expected (when its return type is specialized appropriately) on these examples. 
  Don't worry (yet) about the possibility of minus signs in the specification of natural 
  numbers."
  </code></pre>
  
  <p>Here's my attempt:</p>
  
  <pre><code>data ComplexInt = ComplexInt Int Int
      deriving (Show)
  
  instance Read ComplexInt where
      readsPrec _ = runParser parseComplexInt
  
  parseComplexInt :: Parser ComplexInt
  parseComplexInt = do
      statestring &lt;- getContents
      case statestring of
          if '(' `elem` statestring 
              then do process1 statestring
              else do process2 statestring
      where
      process1 ststr = do
          number &lt;- read(dropWhile (not(isDigit)) ststr) :: Int
          return ComplexInt number 0
      process2 ststr = do
          numbers &lt;- dropWhile (not(isDigit)) ststr
          number1 &lt;- read(takeWhile (not(isSpace)) numbers) :: Int
          number2 &lt;- read(dropWhile (not(isSpace)) numbers) :: Int
          return ComplexInt number1 number2
  </code></pre>
  
  <p>Here's my error (my current error, as I'm sure there will be more once I sort this one out, but I'll take this one step at time):</p>
  
  <pre><code>Parse error in pattern: if ')' `elem` statestring then
                              do { process1 statestring }
                          else
                              do { process2 statestring }
  </code></pre>
  
  <p>I based my structure of the if-then-else statement on the structure used in this question: <a href="https://stackoverflow.com/questions/16846584/parse-error-on-input-in-haskell-if-then-else-conditional">&quot;parse error on input&quot; in Haskell if-then-else conditional</a></p>
  
  <p>I would appreciate any help with the if-then-else block as well as with the code in general, if you see any obvious errors.</p>