Is this efficient for checking multiple statements in Haskell? Or this there a better way?
case ((x > -10) && (x < 20),x /= 9,(x `mod` 2) == 0,x) of
(False,_,_,_) -> error "Not in range"
(_,False,_,_) -> error "Must not be 9"
(_,_,False,_) -> error "Must be even"
(True,True,True,10) -> stuff ()
(True,True,True,20) -> stuff ()
_ -> error "Error Message"
It’s sometimes difficult to come up with small examples of this problem which don’t look contrived, but they do happen. Sometimes you need a bunch of computed results to figure out how to split a function into its cases.
So yes, I often find it’s cleanest to use
caseon a tuple of things-I-might-care-about to build complex decision processes. I trust laziness to compute the minimum required to resolve which branch to invoke.It’s worth trying to express your tests via Boolean guards (or even pattern guards), but sometimes there’s nothing to beat tabulating the computed values you need in a big tuple and then writing a row for each interesting combination of circumstances.