views:

164

answers:

2

I have:

data Color = Blue | Green | Red | White | Yellow deriving (Eq,Ord)

And then

data Term = Color | ...
data Bag = Bag {
color :: Color
...
}

Now I want to be able to pattern match to make sure that the term given is a Color and if so check it's "value" (Blue/Green...). Something like this:

func :: Term -> Bag -> Bool
func (c :: Color) bag = (color bag) == c

But (c :: Color) does not seem to work.

+8  A: 
data Color = Blue | Green | Red | White | Yellow deriving (Eq,Ord)

data Term = Color Color | Trash

data Bag = Bag {
  color :: Color
}

func (Color x) bag = (color bag) == x

-- With the above, a call of func Trash something will fail.
-- (unexhastive pattern match). You can add

func Trash bag = False

-- or

func _ _ = False

-- and it will work all time.
sdcvvc
+2  A: 
Arthur