[Haskell-beginners] find element of tupels
Magnus Therning
magnus at therning.org
Thu Dec 17 10:21:13 EST 2009
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
More information about the Beginners
mailing list