[Haskell-cafe] nooby question on typing
sebastian.sylvan at gmail.com
Sat Sep 13 10:01:41 EDT 2008
On Sat, Sep 13, 2008 at 2:49 PM, Han Joosten <j.m.m.joosten at hccnet.nl>wrote:
> I have a question about types in Haskell. I feel that I am overlooking some
> obvious solution, but I do not manage to get it right. Here is the plot:
> I have got 4 different types of 'rules', each with it's own constructor.
> i defined:
> > type Rules = [Rule]
> > data Rule = RuRule
> > | SgRule
> > | GcRule
> > | FrRule
> > deriving (Eq,Show)
This effectively creates an enum type. I.e. each case here doesn't contain
any data other than the "tag". I think you're getting confused because the
constructor is named the same as the type you're expecting to store. Try
> type Rules = [Rule]
> data Rule = RuRule
> | MkSgRule SgRule
> | MkGcRule GcRule
> | MkFrRule FrRule
> deriving (Eq,Show)
So MkSgRule is a "tag" or a "label" deciding which version of Rule you're
building, and it also has a value of type SgRule.
Now you can create a list or Rule like so:
>mylist :: [Rule]
>mylist = [ MkSgRule mysgrule, MkGcRule mygcrule ]
where mysgrule :: SgRule and mygcrule :: GcRule.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe