[Haskell-beginners] Using symbolic values in the case expression

Daniel Fischer daniel.is.fischer at web.de
Thu Nov 11 13:18:33 EST 2010


On Thursday 11 November 2010 18:45:43, C K Kashyap wrote:
> Is there a way out?
> What I need to do is implement a protocol - so it would be nice to
> write a case expression with patters being symbolic.

Guards?

case expr of
  res | res == v1 -> "One"
      | res == v2 -> "Two"
  _ -> error "Three"

Or perhaps a little preprocessor abuse might help.

{-# LANGUAGE CPP #-}

#define V1 1
#define V2 2

case expr of
  V1 -> "One"
  V2 -> "Two"
  _  -> error "Three"


More information about the Beginners mailing list