[Haskell-beginners] find element of tupels

Daniel Fischer daniel.is.fischer at web.de
Thu Dec 17 18:17:56 EST 2009


Am Donnerstag 17 Dezember 2009 20:44:34 schrieb Henk-Jan van Tuyl:
> On Thu, 17 Dec 2009 17:18:41 +0100, <kane96 at gmx.de> wrote:
> > thanks Magnus, great, works so far. I added to check that myDay have to
> > be greater 0:
> >
> > legalDate :: Date -> Bool
> > legalDate (myDay, myMonth, myYear) = maybe False id $ do
> >     days <- lookup myMonth monthAndMaxDay
> >     return (not (myDay <= 0) && (myDay <= days))
> >
> > Another thing is, that I have to check for leapYears. I have a function
> > that checks if a year is a leap year
> >
> > isLeapYear :: Int -> Bool
> > isLeapYear year = mod year 4 == 0
>
> That is not entirely correct; from package time:
>    isLeapYear :: Integer -> Bool
>    isLeapYear year = (mod year 4 == 0) && ((mod year 400 == 0) || not (mod
> year 100 == 0))
>
> > monthAndMaxDay has 30 days for February which have to be fix. So i have
> > to check in legalDate if the year is a leap year. If so I have to check
> > myDay in February for 29 days, otherwise for 28 days. How can I
> > implement this to isLegalDate?
>
> Use fromGregorianValid from package time.

Or

legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
legalDate (d,m,y)
    | m `elem` [Apr,Jun,Sep,Nov] = 0 < d && d < 31
    | otherwise = 0 < d && d < 32

>
> Regards,
> Henk-Jan van Tuyl
>
>
> --
> http://Van.Tuyl.eu/
> http://members.chello.nl/hjgtuyl/tourdemonad.html
> --



More information about the Beginners mailing list