Search code examples
haskellpattern-matchingtype-conversionparse-erroroption-type

Parse error in a case statement


I am trying to convert a Maybe Int to an Int in Haskell like this:

convert :: Maybe Int -> Int
convert mx = case mx of 
               Just x -> x
              Nothing -> error "error message"

When I compile it, Haskell tells me: parse error on input 'Nothing'.

I need this, because I want to get the Index of an element in a list with the elem.Index function from the Data.List module and then use this index on the take function. My problem is that elemIndex returns a Maybe Int, but take needs an Int.


Solution

  • This is a whitespace problem. The case clauses need to be indented to the same level.

    convert :: Maybe Int -> Int
    convert mx = case mx of 
                   Just x -> x
                   Nothing -> error "error message"
    

    Remember to use only spaces, no tabs.