Patter matching beginner question

Hi all, I assume the following behavior has a trivial explanation. When I write: case name of "a" -> ... "b" -> ... everything works fine. But when I extract a and b to constants: c_a = "a" :: String c_b = "b" :: String case name of c_a -> ... c_b -> ... I get Patterns match(es) are overlapped. Adam

On Dec 16, 2007, at 23:35 , Adam Smyczek wrote:
case name of c_a -> ... c_b -> ... I get Patterns match(es) are overlapped.
You can't use arbitrary expressions in patterns; any name (not a data constructor) used in one creates a new lambda binding (shadowing any existing binding) which receives the value at that point in the pattern. So
case name of c_a -> ...
captures the value of name in a new binding c_a. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Adam Smyczek
But when I extract a and b to constants:
c_a = "a" :: String c_b = "b" :: String
case name of c_a -> ... c_b -> ... I get Patterns match(es) are overlapped.
Do you require 'name' and the constants to be of type String? If not, you could get around the issue Brandon described by declaring a data type for them: data Name = A | B ... case name of A -> ... B -> ... The type system would then prevent A and B from being used in places where a String is expected. This may be an advantage, depending on the style of the rest of the program. Regards, Tom
participants (3)
-
Adam Smyczek
-
Brandon S. Allbery KF8NH
-
Tom Pledger