
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

On Fri, Dec 18, 2009 at 5:01 PM,
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ï

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é"
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] print all days of calendar
On Fri, Dec 18, 2009 at 5:01 PM,
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

Am Sonntag 20 Dezember 2009 22:11:51 schrieb kane96@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".

-------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 22:25:53 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar
Am Sonntag 20 Dezember 2009 22:11:51 schrieb kane96@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

Am Montag 21 Dezember 2009 11:05:03 schrieb kane96@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).

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 11:26:04 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar
Am Montag 21 Dezember 2009 11:05:03 schrieb kane96@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

Am Montag 21 Dezember 2009 11:38:14 schrieb kane96@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

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 11:53:02 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar
Am Montag 21 Dezember 2009 11:38:14 schrieb kane96@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

Am Montag 21 Dezember 2009 17:04:56 schrieb kane96@gmx.de:
-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 11:53:02 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar Am Montag 21 Dezember 2009 11:38:14 schrieb kane96@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) = ??

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 17:22:18 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar
-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 11:53:02 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar Am Montag 21 Dezember 2009 11:38:14 schrieb kane96@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
Am Montag 21 Dezember 2009 17:04:56 schrieb kane96@gmx.de: 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) = ??
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

Am Montag 21 Dezember 2009 17:39:49 schrieb kane96@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?

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 19:54:09 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar
Am Montag 21 Dezember 2009 17:39:49 schrieb kane96@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

Am Montag 21 Dezember 2009 20:47:38 schrieb kane96@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)]

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 21:12:48 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar
Am Montag 21 Dezember 2009 20:47:38 schrieb kane96@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

Am Montag 21 Dezember 2009 21:39:38 schrieb kane96@gmx.de:
-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 21:12:48 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar Am Montag 21 Dezember 2009 20:47:38 schrieb kane96@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.

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 22:00:58 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] print all days of calendar
Am Montag 21 Dezember 2009 21:39:38 schrieb kane96@gmx.de:
-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 21:12:48 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] print all days of calendar Am Montag 21 Dezember 2009 20:47:38 schrieb kane96@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.
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
participants (3)
-
Chaddaï Fouché
-
Daniel Fischer
-
kane96@gmx.de