Syntax Q: How do you share RHS's in case expressions!?
I'm baffled that I can't find anywhere in the documentation how to associate two or more patterns with the same right-hand-side... For example, in OCaml: match 3 with 3 -> 99 | 4 -> 99 Can be abbreviated match 3 with 3 | 4 -> 99 But I have had no luck figuring out how to do the same thing with: case 3 of 3 -> 99; 4 -> 99 My apologies if the answer to this question is blindingly obvious! --Ryan
Ryan Newton wrote:
For example, in OCaml:
match 3 with 3 -> 99 | 4 -> 99
Can be abbreviated
match 3 with 3 | 4 -> 99
But I have had no luck figuring out how to do the same thing with:
case 3 of 3 -> 99; 4 -> 99
You can do this: let rhs = (some complicated expression) in case 3 of 3 -> rhs 4 -> rhs The complicated expression won't be evaluated unless one of the appropriate case alternatives is chosen. -- Ben
On 2004 October 22 Friday 12:15, Ben Rudiak-Gould wrote:
Ryan Newton wrote:
[...] case 3 of 3 -> 99; 4 -> 99
You can do this:
let rhs = (some complicated expression) in case 3 of 3 -> rhs 4 -> rhs
You can also do case 3 of x | x `elem` [3,4] -> 99 though strictly speaking that's not using pattern matching to get the job done. Haskell syntax doesn't provide a way to associate multiple patterns with one right hand side.
I'm baffled that I can't find anywhere in the documentation how to associate two or more patterns with the same right-hand-side...
It gets more interesting with things like: case x of ( ([],a) | (a,[]) ) -> .. a .. where the rewrite looks like: let foo a = ... in case x of ([],a) -> foo a; (a,[]) -> foo a :-( Stefan
participants (4)
-
Ben Rudiak-Gould -
Ryan Newton -
Scott Turner -
Stefan Monnier