
Hi all, Ocaml's match .. with expression (very similar to Haskell's case) allows multiple matches for a single result (naive example): let f x = match x with | 0 -> "zero" | 1 | 3 | 5 | 7 -> "odd" | 2 | 4 | 6 | 8 -> "even" _ -> "bad number" Is there a similar thing in Haskell? At the moment I have to do something like : f x = case x of 0 -> "zero" 1 -> "odd" 3 -> "odd" 5 -> "odd" 7 -> "odd" 2 -> "even" 4 -> "even" 6 -> "even" 8 -> "even" _ -> "bad number" Cheers, Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "One of our programming maxims is "make illegal states unrepresentable" by which we mean that if a given collection of values constitute an error, then it is better to arrange for that collection of values to be impossible to represent within the constraints of the type system." -- Yaron Minsky http://www.haskell.org/sitewiki/images/0/03/TMR-Issue7.pdf

On Thu, Jan 22, 2009 at 10:01 PM, Erik de Castro Lopo
Hi all,
Ocaml's match .. with expression (very similar to Haskell's case) allows multiple matches for a single result (naive example):
let f x = match x with | 0 -> "zero" | 1 | 3 | 5 | 7 -> "odd" | 2 | 4 | 6 | 8 -> "even" _ -> "bad number"
Is there a similar thing in Haskell? At the moment I have to do something like :
f x = case x of 0 -> "zero" 1 -> "odd" 3 -> "odd" 5 -> "odd" 7 -> "odd" 2 -> "even" 4 -> "even" 6 -> "even" 8 -> "even" _ -> "bad number"
Well you can guard each clause: case x of 0 -> "zero" n | even n -> "even" | odd n -> "odd" | otherwise -> "bad number" Still not quite exactly what you've got. So you could really hammer it with blunt force: case x of 0 -> "zero" n | n `elem` [1,3,5,7] -> "odd" | n `elem` [2, 4, 6, 8] -> "even" | otherwise -> "bad number" By the way, zero is even. http://en.wikipedia.org/wiki/Zero_is_even

David Morse wrote:
So you could really hammer it with blunt force:
case x of 0 -> "zero" n | n `elem` [1,3,5,7] -> "odd" | n `elem` [2, 4, 6, 8] -> "even" | otherwise -> "bad number"
Ah, that's was what I was after. Thanks.
By the way, zero is even. http://en.wikipedia.org/wiki/Zero_is_even
I did warn people this was a naive example. Did I really need to make people aware this it was a *silly, completely contrived and possibly incorrect* example ;-). Cheers, Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "Therapists typically base the nuttiness of a patient on the strength of their convictions, on which basis this 43,000 word opus alone stands as a kind of testament to Bill's (Gates) madness." - The Register

On Fri, Jan 23, 2009 at 02:01:41PM +1100, Erik de Castro Lopo wrote:
Hi all,
Ocaml's match .. with expression (very similar to Haskell's case) allows multiple matches for a single result (naive example):
let f x = match x with | 0 -> "zero" | 1 | 3 | 5 | 7 -> "odd" | 2 | 4 | 6 | 8 -> "even" _ -> "bad number"
Is there a similar thing in Haskell?
There isn't, but in this particular case (and in many similar cases) you could always do something like this, using guards: let f x | x == 0 -> "zero" | x `elem` [1,3,5,7] -> "odd" | x `elem` [2,4,6,8] -> "even" | otherwise -> "bad number" -Brent

Erik de Castro Lopo wrote:
Hi all,
Ocaml's match .. with expression (very similar to Haskell's case) allows multiple matches for a single result (naive example):
let f x = match x with | 0 -> "zero" | 1 | 3 | 5 | 7 -> "odd" | 2 | 4 | 6 | 8 -> "even" _ -> "bad number"
Is there a similar thing in Haskell? At the moment I have to do something like :
f x = case x of 0 -> "zero" 1 -> "odd" 3 -> "odd" 5 -> "odd" 7 -> "odd" 2 -> "even" 4 -> "even" 6 -> "even" 8 -> "even" _ -> "bad number"
Cheers, Erik
Hi Erik, you can use guards like this: f x | x == 0 = "zero" | x `elem` [1,3,5,7] = "odd" | x `elem` [2,4,6,8] = "even" | otherwise = "bad number" daniel.
participants (4)
-
Brent Yorgey
-
Daniel Seidel
-
David Morse
-
Erik de Castro Lopo