
Hi, When I compile this code, ghc complains about some overlapped patterns in function depth. What on Earth is ghc talking about? O:-) data BinTree a = EmptyTree | NodeBT a (BinTree a) (BinTree a) deriving Show emptyBT = EmptyTree depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right)

On Jan 13, 2008, at 13:59 , Fernando Rodriguez wrote:
When I compile this code, ghc complains about some overlapped patterns in function depth. What on Earth is ghc talking about? O:-)
emptyBT = EmptyTree
depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right)
If you use a variable in a pattern match, it creates a new binding which irrefutably matches the corresponding argument. In other words, you get a new local variable "emptyBT", ignoring your global. You must use the actual constructor instead. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Hello Brandon S. Allbery KF8NH,
depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right) If you use a variable in a pattern match, it creates a new binding which irrefutably matches the corresponding argument. In other words, you get a new local variable "emptyBT", ignoring your global. You must use the actual constructor instead.
I was wondering why depth always returned zero... Thanks. :-)

Hello Fernando,
Hi,
When I compile this code, ghc complains about some overlapped patterns in function depth. What on Earth is ghc talking about? O:-)
data BinTree a = EmptyTree | NodeBT a (BinTree a) (BinTree a) deriving Show emptyBT = EmptyTree depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right)
Sorry, the exact error is: Warning: Pattern match(es) are overlapped In the definition of `depth': depth (NodeBT _ left right) = ...

On Jan 13, 2008, at 14:05 , Fernando Rodriguez wrote:
depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right)
Sorry, the exact error is: Warning: Pattern match(es) are overlapped In the definition of `depth': depth (NodeBT _ left right) = ...
Right. emptyBT matches anything and stores it in a new local variable "emptyBT", so the second pattern overlaps this. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Fernando Rodriguez writes:
What on Earth is ghc talking about? O:-) (overlapping paterns)
emptyBT = EmptyTree
depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right)
GHC is always right... Your first clause is GENERIC, the pattern is a variable, not a constant. Replace emptyBT by EmptyTree. Jerzy Karczmarczuk
participants (3)
-
Brandon S. Allbery KF8NH
-
Fernando Rodriguez
-
jerzy.karczmarczuk@info.unicaen.fr