[Haskell-cafe] pattern matches are overlapped

瀏覽次數:9 次
跳到第一則未讀訊息

bri...@aracnet.com

未讀,
2014年10月23日 下午2:23:382014/10/23
收件者:haskel...@haskell.org
This works fine

keyboardAct a d p (SpecialKey KeyLeft) Down Modifiers {shift=Down, ctrl=Up, alt=Up} = do
(x,y) <- get p
p $= (x-0.1, y)

This

keyboardAct a d p (SpecialKey KeyLeft) Down shiftDown = do
(x,y) <- get p
p $= (x-0.1, y)

where,

shiftDown = Modifiers {shift=Down, ctrl=Up, alt=Up}

gives me :

KeyBindings.hs:32:1: Warning:
Pattern match(es) are overlapped
In an equation for `keyboardAct':
keyboardAct angle
delta
p
(SpecialKey KeyLeft)
Down
Modifiers {shift = Up, ctrl = Up, alt = Up}
=
...

keyboardAct is used several times via pattern matching, e.g.

keyboardAct angle delta p (SpecialKey KeyLeft) Down Modifiers {shift=Up, ctrl=Up, alt=Up} = do
(x,y) <- get p
a <- get angle
d <- get delta
angle $= a+d

and there are several more.

So clearly, the pattern match is overlapped, just like the warning says.

The question is why I don't get the same warning before I make use of shiftDown.

Thanks
Brian
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Brandon Allbery

未讀,
2014年10月23日 下午2:27:412014/10/23
收件者:bri...@aracnet.com、haskell-cafe
On Thu, Oct 23, 2014 at 2:23 PM, <bri...@aracnet.com> wrote:
keyboardAct a d p (SpecialKey KeyLeft) Down shiftDown = do
  (x,y) <- get p
  p $= (x-0.1, y)

where,

   shiftDown = Modifiers {shift=Down, ctrl=Up, alt=Up}

You can't use a definition like that directly in a pattern match; a name starting with lowercase is a new binding, not the value of some existing binding. You can instead use a guard or, sometimes, a pattern guard.

That is,

    keyboardAct a d p (SpecialKey KeyLeft) Down shiftDown =

is identical to

    keyboardAct a d p (SpecialKey KeyLeft) Down x =

aside from the name of the new local binding it introduces. It does *not* use an existing binding for shiftDown or x, it always creates a new one.

--
brandon s allbery kf8nh                               sine nomine associates
allb...@gmail.com                                  ball...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
回覆所有人
回覆作者
轉寄
0 則新訊息