I开发者_如何转开发s there a valid way to do the following in Haskell:
case n of
0 -> doThis
1 -> doThat
2 -> doAnother
3..99 -> doDefault
other than to have 97 lines of "doDefault" ?
case n of
0 -> doThis
1 -> doThat
2 -> doAnother
_ -> doDefault
If you really need a range,
case n of
0 -> doThis
1 -> doThat
2 -> doAnother
x | 3 <= x && x < 100 -> doDefault
_ -> reallyDoDefault
Using guards! ;)
Foo n
| n == 0 = doThis
| n == 1 = doThat
| n == 2 = doAnother
| (n >= 3 ) && (n <= 99) = doDefault
OR
| n `elem` [3..99] = doDefault
I think you can have the default case be the _ pattern, which matches on anything.
case n of
0 -> doThis
1 -> doThat
2 -> doAnother
_ -> doDefault
I'm not sure if that's quite what you're looking for, since it doesn't check the upper bound on the range there... you might want to use guards instead.
精彩评论