Case expressions, matching, and "constants"

Bayley, Alistair
Thu, 17 Jul 2003 12:03:19 +0100

I've just debugged a program that used a case expression, but where I was
trying to match on constants rather than literals. Here's a contrived

> module Main where
> one = 1
> two = 2
> test n =
>	case n of
>		one -> "one"
>		two -> "two"
>		_ -> "three"
> main = putStrLn (test 2)

This initial version seems to me to be the "natural" way to write the case
expression, but it doesn't work because the first alternative always

This is what I've turned it into to get it to work. It seems a bit clumsy;
is there a better way to write this?

> test n =
>	case True of
>		_ | n == one -> "one"
>		  | n == two -> "two"
>		  | otherwise -> "three"

The information in this email and in any attachments is 
confidential and intended solely for the attention and use 
of the named addressee(s). This information may be 
subject to legal professional or other privilege or may 
otherwise be protected by work product immunity or other 
legal rules.  It must not be disclosed to any person without 
our authority.

If you are not the intended recipient, or a person 
responsible for delivering it to the intended recipient, you 
are not authorised to and must not disclose, copy, 
distribute, or retain this message or any part of it.