Easy syntax question

Dean Herington [email protected]
Mon, 27 Aug 2001 15:09:00 -0400


Mark Carroll wrote:

> I can write a function x :: Integer -> Integer that returns 5 if I give it
> 2, 5 or 9, or 7 otherwise. Say,
>
> x 2 = 5
> x 5 = 5
> x 9 = 5
> x _ = 7
>
> Generally, this is a question about where multiple cases lead to the same
> thing, maybe even in the middle of a function. (Like C's "case 1: case 2:
> case 3: foo; break;".)
>
> Does it get any better than this, though? I can't convince 'case' to do
> something like,
>
> case n of
>   2,5,9 -> 5
>   otherwise -> 7
>
> Am I missing some syntax somewhere? I'm lost in the grammar in the Haskell
> report.
>
> -- Mark

I would write:

> if elem n [2,5,9] then 5 else 7

Dean