
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)' -- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser

On Thu, Dec 17, 2009 at 3:03 PM, kane96
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

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
An: kane96 CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] find element of tupels
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)
On Thu, Dec 17, 2009 at 3:03 PM, kane96
wrote: 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

On Thu, 17 Dec 2009 17:18:41 +0100,
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. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html -- --

Am Donnerstag 17 Dezember 2009 20:44:34 schrieb Henk-Jan van Tuyl:
On Thu, 17 Dec 2009 17:18:41 +0100,
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 --

On Fri, 18 Dec 2009 00:17:56 +0100, Daniel Fischer
legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
This should be: legalDate (d, Feb, y) = 0 < d && d < (if isLeapYear y then 29 else 28)
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 --

Am Freitag 18 Dezember 2009 12:01:53 schrieb Henk-Jan van Tuyl:
On Fri, 18 Dec 2009 00:17:56 +0100, Daniel Fischer
wrote: legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
This should be: legalDate (d, Feb, y) = 0 < d && d < (if isLeapYear y then 29 else 28)
No, I test for strict inequality, hence 30 resp. 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

thx Daniel. Now I implemented it like that to use my monthAndMaxDay list and just use the isLeapYear check for February: legalDate :: Date -> Bool legalDate (myDay, Feb, myYear) = 0 < myDay && myDay <= (if isLeapYear myYear then 29 else 28) legalDate (myDay, myMonth, myYear) = maybe False id $ do days <- lookup myMonth monthAndMaxDay return (not (myDay <= 0) && (myDay <= days)) -------- Original-Nachricht --------
Datum: Fri, 18 Dec 2009 00:17:56 +0100 Von: Daniel Fischer
An: beginners@haskell.org Betreff: Re: [Haskell-beginners] find element of tupels
Am Donnerstag 17 Dezember 2009 20:44:34 schrieb Henk-Jan van Tuyl:
On Thu, 17 Dec 2009 17:18:41 +0100,
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 --
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

On 18/12/09 14:57, kane96@gmx.de wrote:
thx Daniel. Now I implemented it like that to use my monthAndMaxDay list and just use the isLeapYear check for February:
legalDate :: Date -> Bool legalDate (myDay, Feb, myYear) = 0 < myDay && myDay <= (if isLeapYear myYear then 29 else 28) legalDate (myDay, myMonth, myYear) = maybe False id $ do days <- lookup myMonth monthAndMaxDay return (not (myDay <= 0) && (myDay <= days))
I know it's a minor detail, but wouldn't it be clearer to change that last line to read something like this: return $ 0 < myDay && myDay <= days ? /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

-------- Original-Nachricht --------
Datum: Thu, 17 Dec 2009 20:44:34 +0100 Von: "Henk-Jan van Tuyl"
An: kane96@gmx.de, beginners@haskell.org Betreff: Re: [Haskell-beginners] find element of tupels
On Thu, 17 Dec 2009 17:18:41 +0100,
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))
ok
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.
but the problem is I shall implement it on my own in this exercise
Regards, Henk-Jan van Tuyl
-- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --
--
-- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser

On Thu, Dec 17, 2009 at 04:03:59PM +0100, kane96 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)'
I'm sure the error message goes on to say something like: in the first argument of (.) namely (find ((== myMonth) . fst) monthAndMaxDay) ... you have to interpret this error message a little bit. It is saying the composition operator (.) is expecting a function of type b -> c in it's first argument (or on the left-hand side), but you have provided something of type Maybe (Month, Day). This type is what that (find...) evaluates to. I'm missing the correct terminology here, but you cannot provide something that is fully evaluable on the left-hand side of (.). It needs to be something that evaluates to a function of one argument. That argument will be supplied by the function provided on the right hand side of (.). So, at a minimum, you need to swap your snd and your (find...), but that doesn't solve the problem of getting the tuple out of the Maybe monad that find returns. A
participants (6)
-
Andrew Sackville-West
-
Daniel Fischer
-
Henk-Jan van Tuyl
-
kane96
-
kane96@gmx.de
-
Magnus Therning