Parse error in pattern [GHC-07626]
This error occurs when the compiler is unable to understand a pattern that you’re attempting to match on.
Example text
Parse error in pattern: merge
Examples
Parse error in pattern
This error occurs because we haven’t properly pattern matched on the list in our definition of the merge
function. In particular, the parentheses are missing. The type here is a list, which means our options are to match on either the empty list, []
, or a list with head and tail, (a:as)
. By omitting the parentheses around a:as
, the compiler reads this as an argument, a
, followed by a misplaced infix cons (:)
operator, rather than considering the :
to be part of the list being matched.
Error Message
Example.hs:5:1: error: [GHC-07626] Parse error in pattern: merge
|
5 | merge a:as (b:bs) rest
| ^^^^^^^
Example.hs
Before
module Example where
merge [] bs rest = bs ++ rest
merge as [] rest = as ++ rest
merge a:as (b:bs) rest
| a > b = merge as (b:bs) (a:rest)
| otherwise = merge (a:as) bs (b:rest)
After
module Example where
merge [] bs rest = bs ++ rest
merge as [] rest = as ++ rest
merge (a:as) (b:bs) rest
| a > b = merge as (b:bs) (a:rest)
| otherwise = merge (a:as) bs (b:rest)