<br><div><span class="gmail_quote">On 8/17/07, <b class="gmail_sendername">rodrigo.bonifacio</b> <<a href="mailto:firstname.lastname@example.org">email@example.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all.<br><br>I want to create the following polymorphic type (EnvItem) that we can apply two functions (envKey and envValue). I tried the following:<br><br>> type Key = String<br><br>> data EnvItem a = EnvItem (Key, a)
<br><br>> envKey :: EnvItem (Key, a) -> String<br>> envKey EnvItem (key, value) = key<br><br>> envValue :: EnvValue(Key, a) -> a<br>> envValue EnvItem (key, value) = value<br><br>But this is resulting in the error: [Constructor "EnvItem" must have exactly 1 argument in pattern]
<br><br>I think this is a very basic problem, but I don't know what is wrong.<br><br>Regards,<br><br>Rodrigo.</blockquote><div><br>By the way, I would suggest giving the data type and constructor different names:<br><br>
data EnvItem a = EI (Key, a)<br><br>You do often see people use the same name for both, but that can be confusing since they are really two different things. The envKey function (for example) would now be written like this:
<br><br>envKey :: EnvItem a -> Key <br></div>envKey (EI (key, _)) = key<br><br>The difference between the parameter type (EnvItem a) and a pattern to match the shape of such a value (EI (key, _)) is now much clearer: whatever is on the left side of the data declaration is the type, and goes in type signatures; whatever is on the right side describes the shape of values of that type, and is used to construct or deconstruct (through pattern-matching) such values. This way it is much harder to make mistakes like (for example) putting EnvItem (Key, a) in the type signature instead of EnvItem a.