[Haskell-cafe] Basic question....
Bryan Burgers
bryan.burgers at gmail.com
Fri Aug 17 13:24:01 EDT 2007
On 8/17/07, rodrigo.bonifacio <rodrigo.bonifacio at uol.com.br> wrote:
> Hi all.
>
> I want to create the following polymorphic type (EnvItem) that we can apply two functions (envKey and envValue). I tried the following:
>
> > type Key = String
>
> > data EnvItem a = EnvItem (Key, a)
>
> > envKey :: EnvItem (Key, a) -> String
> > envKey EnvItem (key, value) = key
>
> > envValue :: EnvValue(Key, a) -> a
> > envValue EnvItem (key, value) = value
>
> But this is resulting in the error: [Constructor "EnvItem" must have exactly 1 argument in pattern]
>
> I think this is a very basic problem, but I don't know what is wrong.
>
> Regards,
>
> Rodrigo.
In addition to what others have already said, I'd like to point out
that you do not really need a tuple in your data item.
> data EnvItem a = EI Key a
> envKey :: EnvItem a -> Key
> envKey (EI key _) = key
> envValue :: EnvValue a -> a
> envValue (EI _ value) = value
Also, you made a distinction between 'Key' and 'String', which is
good. But then in 'envKey', you used 'String' in the signature instead
of 'Key'.That's a little confusing, and also should you ever want to
change the representation of 'Key', you would then have to change the
signature of envKey.
Just my two cents,
Bryan
More information about the Haskell-Cafe
mailing list