Haskell: Non-exhaustive patterns in function checkDirecRio

The next file complile but when I call the checkDirecRio it gives me :Non-exhaustive patterns in function checkDirecRio


checkDirecRio :: Mapa -> Bool
checkDirecRio (Mapa _ []) = True
checkDirecRio (Mapa larg ((Rio x, _) : (_,_)  : t)) = checkDirecRio (Mapa larg t)
checkDirecRio (Mapa larg ((Rio x, _) : (Rio y, _) : t)) | ((x > 0 && y < 0) || (x < 0 && y > 0)) = True
                                                        | otherwise = checkDirecRio (Mapa larg t)


which the structure Mapa is defined as:


type Velocidade = Int

data Mapa =
  Mapa Largura [(Terreno, [Obstaculo])]
  deriving (Show, Read, Eq)


data Terreno
  = Rio Velocidade
  | Estrada Velocidade
  | Relva
  deriving (Show, Read, Eq)


data Obstaculo
  = Nenhum -- ^ nothing
  | Tronco -- ^ wood who can only in Rio being
  | Carro -- ^ car who can only in Estrada being
  | Arvore -- ^ tree who can only in Relva being
  deriving (Show, Read, Eq)


I expect that the code can be able to validate if the river "Rio" has different direction, return False if 2 contiguos rivers has the same direction asn True if contiguous rives has opposite directions

>Solution :

The function checkDirecRio has incomplete patterns because it does not cover all the variants of Terreno which has 2 more variants besides Rio. You should include the case that covers them as well. For example:

checkDirecRio (Mapa _ (((Estrada _), _):_)) = True
checkDirecRio (Mapa _ ((Relva, _):_)) = False
checkDirecRio (Mapa _ [((Rio _), _)]) = False

Leave a Reply