[Haskell-beginners] The case expression

Erik de Castro Lopo mle+cl at mega-nerd.com
Thu Jan 22 22:01:41 EST 2009


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


More information about the Beginners mailing list