Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading