[Haskell-beginners] find element of tupels

kane96 at gmx.de kane96 at gmx.de
Thu Dec 17 11:18:41 EST 2009


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

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?


-------- Original-Nachricht --------
> Datum: Thu, 17 Dec 2009 15:21:13 +0000
> Von: Magnus Therning <magnus at therning.org>
> An: kane96 <kane96 at gmx.de>
> CC: beginners at haskell.org
> Betreff: Re: [Haskell-beginners] find element of tupels

> On Thu, Dec 17, 2009 at 3:03 PM, kane96 <kane96 at gmx.de> wrote:
> > Hi,
> > I have a list of tuples: [(Jan, 31),(Feb, 28),(Mar, 31),...] called
> monthAndMaxDay
> >
> > Date is:
> > type Day = Int
> > data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct |
> Nov | Dec deriving (Eq,Enum,Show)
> > type Year = Int
> > type Date = (Day,Month,Year)
> >
> > Now I want to check if a date is legal:
> > legalDate :: Date -> Bool
> > legalDate (myDay, myMonth, myYear) =
> >    not (myDay <= 0) && myDay >=  (find ((== myMonth) . fst)
> monthAndMaxDay) . snd
> >
> > the find works to search for the month in the first element of every
> tupel but at the end I have to check the second value of the tupel (the day)
> that it issn't higher than the maximal number of days in that month. Where
> do I have to set the "snd" correctly. Here I get the error: Couldn't match
> expected type `b -> c'
> >           against inferred type `Maybe (Month, Day)'
> 
> 
> First of all I'd take a look at the function Prelude.lookup, it'll be
> useful in this case.
> 
> Using that function I'd do something like this:
> 
> legalDate (myDay, myMonth, myYear) = maybe False id $ do
>     days <- lookup myMonth monthAndMaxDay
>     return (myDay <= days)
> 
> /M
> 
> -- 
> Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
> magnus@therning.org          Jabber: magnus@therning.org
> http://therning.org/magnus         identi.ca|twitter: magthe

-- 
Preisknaller: GMX DSL Flatrate für nur 16,99 Euro/mtl.!
http://portal.gmx.net/de/go/dsl02


More information about the Beginners mailing list