[Haskell-cafe] Problem about pattern matching.

minh thu noteed at gmail.com
Wed Apr 28 05:02:02 EDT 2010

2010/4/28 Magicloud Magiclouds <magicloud.magiclouds at gmail.com>:
> Hi, I have code as below. How come "case" version works wrong and
> gives me "overlap" compiling warning? Thanks.
>  if dayOfMonth == firstDayOfMonth
>    then v day (x, y)
>    else if dayOfMonth == lastDayOfMonth
>      then not_ $ v day (x, y)
>      else Mider day (x, y)
>  case dayOfMonth of
>    firstDayOfMonth -> v day (x, y)
>    lastDay -> not_ $ v day (x, y)
>    _ -> Mider day (x, y)


firstDayIfMonth and lastDay are new variables, not previously bound
variable. This means that trying to pattern match (the value of)
dayOfMonth will always succeed with the first alternative.

Think of parsing command line argument, using the last alternative as
a fall-through :
args <- getArgs
case args of
  ["--help"] -> ...
  ["--run-tests"] -> ...
  x -> putStrLn $ "unknow args " ++ concat x

What would happen if the last alternative (with the x) was put in
first position ?


More information about the Haskell-Cafe mailing list