vendredi 2 juin 2017

Haskell Non-exhaustive patterns in function error in Maybe?

I am writing a function that takes a list of Dates and evaluates to an (Int, Int, Int). If the list is empty is evaluates to Nothing and evaluates to Just d where d is the oldest Date in the list. My Date data structure is below:

data Date = Date Int Int Int
 deriving (Show)

Here is a helper function that I use to determine if a Date is older:

isOlder2 :: Date -> Maybe Date -> Bool
isOlder2 (Date x y z) (Just (Date a b c))
  | x < a = True
  | x > a = False
  | (x == a)&&(y < b) = True
  | (x == a)&&(y > b) = False
  | (x == a) && (y ==b) &&(z < c) =True
  | (x == a) && (y ==b) &&(z > c) = False
  | (x == a) && (y ==b) &&(z == c) = False 

and my actual method, oldest, is the following:

oldest :: [Date] -> Maybe Date 
oldest [] = Nothing
oldest (x : xs)
  | isOlder2 x oldestTail = Just x
  | otherwise = oldestTail 
    where oldestTail = oldest xs

I think that the issue is somewhere in the Maybe, but I'm not familiar with the structure and how it works. Any thoughts?

Aucun commentaire:

Enregistrer un commentaire