Easy syntax question
Mike Gunter
m@ryangunter.com
27 Aug 2001 12:44:45 -0700
Or, if you need several cases, you can use something like:
> funcGuard n | n `elem` [2,5,9] = 5
> | n `elem` [4,18] = 1
> | otherwise = 7
or, if you want an expression:
> caseGuard n = case undefined of
> _ | n `elem` [2,5,9] -> 5
> | n `elem` [4,18] -> 1
> | otherwise -> 7
mike
Dean Herington <heringto@cs.unc.edu> writes:
> 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