Haskell pattern matching - how to use constants variable

Viewed 5335

I would ideally like to write something like this:

myValue1 = 1 :: Int
myValue2 = 2 :: Int

myFunc :: Int -> Bool
myFunc myValue1 = True
myFunc myValue2 = False

Calling myFunc myValue2 returns True - not what I intend. I know why this happens, but is there a way to express this in Haskell without resorting to C-style #define statements?

4 Answers

If the idea is just to define some constants to use in patterns, you can also use the language extension PatternSynonyms:

{-# LANGUAGE PatternSynonyms #-}

pattern MyValue1 = 1
pattern MyValue2 = 2

myFunc :: Int -> Bool
myFunc MyValue1 = True
myFunc MyValue2 = False
Related