[Haskell-beginners] find element of tupels

Andrew Sackville-West andrew at swclan.homelinux.org
Thu Dec 17 11:09:10 EST 2009

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. 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/beginners/attachments/20091217/f14541dd/attachment.bin

More information about the Beginners mailing list