[Haskell-cafe] Problem about pattern matching.

minh thu noteed at gmail.com
Thu Apr 29 05:01:04 EDT 2010


Hi,

Precisely, the fact that you already defined them is not important:
variables that appear in the left-hand-side of a production are new,
local variables, just as it would be the case with nested lets:

let x = 1 in let x = 2 in x -- what's the value of x ?

let x = 1
in case 2 of
      x -> x  -- what's the value of x ?

now, in the expression

case 1 of
  x -> ...
  y -> ...
  _ -> ...

every left-hand-side can pattern-match succesfully against the value
1, but since only the first match is used, y and _ are useless. Since
they match identical values (in this case, they match anything), they
are overlapped.

Make sure you can tell what the value of the x above are and explain why.

Cheers,
Thu


2010/4/29 Magicloud Magiclouds <magicloud.magiclouds at gmail.com>:
> Sorry folks, I did not make it clear. The firstDayOfMonth and
> lastDayOfMonth was defined earlier in let ... in structure.
> The problem here is, ghc warned me that "_" and "lastDayOfMonth" was
> overlapped. And the results showed that, everything that should be
> worked with "Mider" was done by "not_ ...".
>
> On Thu, Apr 29, 2010 at 1:51 AM, Henning Thielemann
> <schlepptop at henning-thielemann.de> wrote:
>> minh thu schrieb:
>>> 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)S
>>>>    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)
>>>
>>> Hi,
>>>
>>> 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.
>>
>> You can however simulate a 'case' with predefined variables:
>>   http://haskell.org/haskellwiki/Case
>>
>
>
>
> --
> 竹密岂妨流水过
> 山高哪阻野云飞
>


More information about the Haskell-Cafe mailing list