I'm trying to pattern match against a ratio:
isValid :: Ratio Int -> Bool
isValid (num % den) = ...
However, this yields:
Parse error in pattern: num % den
Interestingly, the Data.Ratio
package defines the numerator
and denominator
functions in this way, but with the :%
operator:
numerator (x :% _) = x
denominator (_ :% y) = y
However, I don't have access to this latter operator.
Could anyone explain why my pattern match doesn't work and how I could fix it?
While in this case you can just import the operator from GHC.Ratio
, there is a general solution starting from GHC 7.8: pattern synonyms.
pattern num :% denom <- ((\x -> (numerator x, denominator x)) -> (num, denom))