Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-beginners] print all days of calendar

67 views
Skip to first unread message

kan...@gmx.de

unread,
Dec 18, 2009, 11:01:16 AM12/18/09
to begi...@haskell.org
I have to create a calendar like this:
type Calendar = [Date]

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)

I shall create a function: calendar :: Year -> Calendar that return a calendar for the given year like:
[(1, Jan, 2009), (2, Jan, 2009), ..., (31, Dec, 2009)]

I also have a function from a previous exercise which checks if a given date is valid. Is there a function for a loop that iterates from 1 to n and checks if the date is valid. If it's valid it should return the date otherwise it should jump to the next month or end at the end of the year? Or is it better to do it on another way with this data I have?


--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
_______________________________________________
Beginners mailing list
Begi...@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

kan...@gmx.de

unread,
Dec 20, 2009, 4:12:04 PM12/20/09
to begi...@haskell.org
I implemented the first step which works if I fix the months (instead of the Month Enum) and just return the day and not an element of type Date

this works:
calendar year = [ day | month <- [Jan, Feb], day <- [1..31], legalDate(day,month,year) == True ]

this doesn't work:
calendar year = [ Date(day,month,year) | month <- Month, day <- [1..31], legalDate(day,month,year) == True ]


-------- Original-Nachricht --------
> Datum: Sat, 19 Dec 2009 11:16:45 +0100
> Von: "Chadda� Fouch�" <chaddai...@gmail.com>
> An: kan...@gmx.de
> CC: begi...@haskell.org
> Betreff: Re: [Haskell-beginners] print all days of calendar

> On Fri, Dec 18, 2009 at 5:01 PM, <kan...@gmx.de> wrote:
> > I also have a function from a previous exercise which checks if a given
> date is valid. Is there a function for a loop that iterates from 1 to n and
> checks if the date is valid. If it's valid it should return the date
> otherwise it should jump to the next month or end at the end of the year? Or is
> it better to do it on another way with this data I have?
>

> Since you derived Enum for Month, you can do [Jan..Dec] and get the
> list of the months in order. There are then two options, either you
> generate all cartesian product of [Jan..Dec] and [1..31] and check
> which are valid, or you write a function that for a given month and
> year tells you how many days it counts and then generate for month "m"
> all the pair in combination with [1..daysCount m].
>
> Whatever your decision, list comprehensions are probably the tool of
> choice to do it though it is by no mean harder to do without.
>
> --
> Jeda�

--
GRATIS f�r alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Daniel Fischer

unread,
Dec 20, 2009, 4:27:32 PM12/20/09
to begi...@haskell.org
Am Sonntag 20 Dezember 2009 22:11:51 schrieb kan...@gmx.de:
> I implemented the first step which works if I fix the months (instead of
> the Month Enum) and just return the day and not an element of type Date
>
> this works:
> calendar year = [ day | month <- [Jan, Feb], day <- [1..31],
> legalDate(day,month,year) � � == True ]
>
> this doesn't work:
> calendar year = [ Date(day,month,year) | month <- Month, day <- [1..31],
> legalDate(day,month,year) � � == True ]

Date was a type synonym for the triple, so
calendar year
= [ (day,month,year) | month <- Month, day <- [1..31], legalDate(day,month,year)]

should be what you want.
Note, "condition == True" is better written as "condition".

kan...@gmx.de

unread,
Dec 21, 2009, 5:05:15 AM12/21/09
to begi...@haskell.org
-------- Original-Nachricht --------
> Datum: Sun, 20 Dec 2009 22:25:53 +0100
> Von: Daniel Fischer <daniel.i...@web.de>
> An: begi...@haskell.org
> CC: kan...@gmx.de

> Betreff: Re: [Haskell-beginners] print all days of calendar

> Am Sonntag 20 Dezember 2009 22:11:51 schrieb kan...@gmx.de:


> > I implemented the first step which works if I fix the months (instead of
> > the Month Enum) and just return the day and not an element of type Date
> >
> > this works:
> > calendar year = [ day | month <- [Jan, Feb], day <- [1..31],
> > legalDate(day,month,year) � � == True ]
> >
> > this doesn't work:
> > calendar year = [ Date(day,month,year) | month <- Month, day <- [1..31],
> > legalDate(day,month,year) � � == True ]
>
> Date was a type synonym for the triple, so
> calendar year
> = [ (day,month,year) | month <- Month, day <- [1..31],
> legalDate(day,month,year)]
>
> should be what you want.
> Note, "condition == True" is better written as "condition".

still have the problem that it says:
"Not in scope: data constructor `Month'" but Month is declared as:


data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct | Nov | Dec deriving (Eq,Enum,Show)

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

Daniel Fischer

unread,
Dec 21, 2009, 5:27:46 AM12/21/09
to begi...@haskell.org
Am Montag 21 Dezember 2009 11:05:03 schrieb kan...@gmx.de:
>
> still have the problem that it says:
> "Not in scope: data constructor `Month'" but Month is declared as:
> data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct |
> Nov | Dec deriving (Eq,Enum,Show)

Sorry, didn't look closely enough.
Month is the name of the datatype. You want month to run through all elements of Month, so
you need a list, namely [Jan .. Dec] (it might be necessary to include Ord in the derived
instances for Month).

kan...@gmx.de

unread,
Dec 21, 2009, 5:38:27 AM12/21/09
to begi...@haskell.org

-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 11:26:04 +0100

> Von: Daniel Fischer <daniel.i...@web.de>
> An: begi...@haskell.org
> CC: kan...@gmx.de
> Betreff: Re: [Haskell-beginners] print all days of calendar

> Am Montag 21 Dezember 2009 11:05:03 schrieb kan...@gmx.de:


> >
> > still have the problem that it says:
> > "Not in scope: data constructor `Month'" but Month is declared as:
> > data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct |
> > Nov | Dec deriving (Eq,Enum,Show)
>
> Sorry, didn't look closely enough.
> Month is the name of the datatype. You want month to run through all
> elements of Month, so
> you need a list, namely [Jan .. Dec] (it might be necessary to include Ord
> in the derived
> instances for Month).

ahhh, it works in the right order. I tried [Jan..Dec] (without the spaces) which failed.

Which function can I use to trim the calendar of a month from the calendar of the whole year, because I have to implement a function:
getMonth :: Month -> Calendar -> Calendar

--
GRATIS f�r alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

kan...@gmx.de

unread,
Dec 21, 2009, 11:05:14 AM12/21/09
to begi...@haskell.org

-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 11:53:02 +0100

> Von: Daniel Fischer <daniel.i...@web.de>
> An: begi...@haskell.org
> CC: kan...@gmx.de
> Betreff: Re: [Haskell-beginners] print all days of calendar

> Am Montag 21 Dezember 2009 11:38:14 schrieb kan...@gmx.de:
> > Which function can I use to trim the calendar of a month from the
> calendar
> > of the whole year, because I have to implement a function: getMonth ::
> > Month -> Calendar -> Calendar
>

> look at filter or span and break, takeWhile/dropWhile

Dont't find any good examples of this functions that can help me. I think it have to be somethink like that, but don't find a way that works:


getMonth :: Month -> Calendar -> Calendar

getMonth month year = filter ([1..31],month,year) (calendar year)


--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -

sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser

Daniel Fischer

unread,
Dec 21, 2009, 11:24:00 AM12/21/09
to begi...@haskell.org
Am Montag 21 Dezember 2009 17:04:56 schrieb kan...@gmx.de:
> -------- Original-Nachricht --------
>
> > Datum: Mon, 21 Dec 2009 11:53:02 +0100
> > Von: Daniel Fischer <daniel.i...@web.de>
> > An: begi...@haskell.org
> > CC: kan...@gmx.de
> > Betreff: Re: [Haskell-beginners] print all days of calendar
> >
> > Am Montag 21 Dezember 2009 11:38:14 schrieb kan...@gmx.de:
> > > Which function can I use to trim the calendar of a month from the
> >
> > calendar
> >
> > > of the whole year, because I have to implement a function: getMonth ::
> > > Month -> Calendar -> Calendar
> >
> > look at filter or span and break, takeWhile/dropWhile
>
> Dont't find any good examples of this functions that can help me. I think
> it have to be somethink like that, but don't find a way that works:
> getMonth :: Month -> Calendar -> Calendar
> getMonth month year = filter ([1..31],month,year) (calendar year)

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

So we need a predicate on the type of list elements.

getMonth month cal = filter predicate cal
where
predicate (d,m,y) = ??

kan...@gmx.de

unread,
Dec 21, 2009, 12:06:31 PM12/21/09
to begi...@haskell.org

-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 17:22:18 +0100

it has to match the given month and year. But don't know how to define it.

--
GRATIS f�r alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Daniel Fischer

unread,
Dec 21, 2009, 1:55:55 PM12/21/09
to begi...@haskell.org
Am Montag 21 Dezember 2009 17:39:49 schrieb kan...@gmx.de:
> > Prelude> :t filter
> > filter :: (a -> Bool) -> [a] -> [a]
> >
> > So we need a predicate on the type of list elements.
> >
> > getMonth month cal = filter predicate cal
> > � � � where
> > � � � � predicate (d,m,y) = ??
>
> it has to match the given month and year. But don't know how to define it.

Does (==) ring a few bells?

kan...@gmx.de

unread,
Dec 21, 2009, 2:47:58 PM12/21/09
to begi...@haskell.org
-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 19:54:09 +0100

> Von: Daniel Fischer <daniel.i...@web.de>
> An: begi...@haskell.org
> CC: kan...@gmx.de
> Betreff: Re: [Haskell-beginners] print all days of calendar

> Am Montag 21 Dezember 2009 17:39:49 schrieb kan...@gmx.de:


> > > Prelude> :t filter
> > > filter :: (a -> Bool) -> [a] -> [a]
> > >
> > > So we need a predicate on the type of list elements.
> > >
> > > getMonth month cal = filter predicate cal
> > > � � � where
> > > � � � � predicate (d,m,y) = ??
> >
> > it has to match the given month and year. But don't know how to define
> it.
>
> Does (==) ring a few bells?

not really...


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

Daniel Fischer

unread,
Dec 21, 2009, 3:14:31 PM12/21/09
to begi...@haskell.org
Am Montag 21 Dezember 2009 20:47:38 schrieb kan...@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)]

kan...@gmx.de

unread,
Dec 21, 2009, 3:39:56 PM12/21/09
to Daniel Fischer, begi...@haskell.org

-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 21:12:48 +0100

> Von: Daniel Fischer <daniel.i...@web.de>
> An: begi...@haskell.org
> CC: kan...@gmx.de
> Betreff: Re: [Haskell-beginners] print all days of calendar

> Am Montag 21 Dezember 2009 20:47:38 schrieb kan...@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


--
GRATIS f�r alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Daniel Fischer

unread,
Dec 21, 2009, 4:02:41 PM12/21/09
to kan...@gmx.de, begi...@haskell.org
Am Montag 21 Dezember 2009 21:39:38 schrieb kan...@gmx.de:
> -------- Original-Nachricht --------
>
> > Datum: Mon, 21 Dec 2009 21:12:48 +0100
> > Von: Daniel Fischer <daniel.i...@web.de>
> > An: begi...@haskell.org
> > CC: kan...@gmx.de
> > Betreff: Re: [Haskell-beginners] print all days of calendar
> >
> > Am Montag 21 Dezember 2009 20:47:38 schrieb kan...@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.

kan...@gmx.de

unread,
Dec 21, 2009, 5:02:19 PM12/21/09
to Daniel Fischer, begi...@haskell.org

-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 22:00:58 +0100
> Von: Daniel Fischer <daniel.i...@web.de>
> An: kan...@gmx.de
> CC: begi...@haskell.org

so I have to say that it have to match (Day, month, Year), but how...

--
GRATIS f�r alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

0 new messages