[Haskell-beginners] print all days of calendar

Daniel Fischer daniel.is.fischer at web.de
Mon Dec 21 16:00:58 EST 2009


Am Montag 21 Dezember 2009 21:39:38 schrieb kane96 at gmx.de:
> -------- Original-Nachricht --------
>
> > Datum: Mon, 21 Dec 2009 21:12:48 +0100
> > Von: Daniel Fischer <daniel.is.fischer at web.de>
> > An: beginners at haskell.org
> > CC: kane96 at gmx.de
> > Betreff: Re: [Haskell-beginners] print all days of calendar
> >
> > Am Montag 21 Dezember 2009 20:47:38 schrieb kane96 at gmx.de:
> > > > Does (==) ring a few bells?
> > >
> > > not really...
> >
> > Prelude> let okay k = k^3 `mod` 13 == 5 in filter okay [1 .. 30]
> > [7,8,11,20,21,24]
> >
> > Now, you don't want the numbers between 1 and 30 inclusive whose cube
> > modulo 13 is 5, but
> > the dates in a given month.
> >
> > getMonth :: Month -> Calendar -> Calendar
> > getMonth month [(1,Jan,y),(2,Jan,y) ... (31,Dec,y)]
>
> more like this:
> getMonth month calendar = calendar filter ([1..31], month, year)
> but it doesn't make sense

No.
type Calendar = [Date]
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)

So the calendar in "getMonth month calendar" is a list of date-triples as illustrated 
above.

Prelude> :t filter
filter :: (a -> Bool) -> [a] -> [a]

So filter takes
1) a predicate (a function of type (a -> Bool))
2) a list
as arguments.

getMonth :: Month -> [Date] -> [Date]

One of the arguments to getMonth is a list, and the result should be a list of the same 
type, thus it's natural to pass that list unchanged to filter.

getMonth month calendar = filter predicateThatYouNeed calendar

What remains is to define predicateThatYouNeed. It will somehow involve the other argument 
to getMonth, namely month. And it must map (Day,Month,Year) triples to Bool.


More information about the Beginners mailing list