Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  find element of tupels ([email protected])
   2. Re:  find element of tupels ([email protected])
   3.  print all days of calendar ([email protected])
   4. Re:  Enum for natural numbers (Deniz Dogan)
   5. Re:  Enum for natural numbers ([email protected])
   6. Re:  Enum for natural numbers (Deniz Dogan)
   7.  inserting tupels inside list as tupels in list ([email protected])
   8. Re:  find element of tupels (Magnus Therning)
   9. Re:  inserting tupels inside list as tupels in list (Brent Yorgey)
  10.  Fwd: Type constraints question (David Gordon)


----------------------------------------------------------------------

Message: 1
Date: Fri, 18 Dec 2009 15:35:52 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] find element of tupels
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"


-------- Original-Nachricht --------
> Datum: Thu, 17 Dec 2009 20:44:34 +0100
> Von: "Henk-Jan van Tuyl" <[email protected]>
> An: [email protected], [email protected]
> Betreff: Re: [Haskell-beginners] find element of tupels

> On Thu, 17 Dec 2009 17:18:41 +0100, <[email protected]> wrote:
> 
> > thanks Magnus, great, works so far. I added to check that myDay have to 
> > be greater 0:
> >
> > legalDate :: Date -> Bool
> > legalDate (myDay, myMonth, myYear) = maybe False id $ do
> >     days <- lookup myMonth monthAndMaxDay
> >     return (not (myDay <= 0) && (myDay <= days))
> >
> > Another thing is, that I have to check for leapYears. I have a function 
> > that checks if a year is a leap year
> >
> > isLeapYear :: Int -> Bool
> > isLeapYear year = mod year 4 == 0
> 
> That is not entirely correct; from package time:
>    isLeapYear :: Integer -> Bool
>    isLeapYear year = (mod year 4 == 0) && ((mod year 400 == 0) || not (mod
>  
> year 100 == 0))

ok


> 
> 
> >
> > monthAndMaxDay has 30 days for February which have to be fix. So i have 
> > to check in legalDate if the year is a leap year. If so I have to check 
> > myDay in February for 29 days, otherwise for 28 days. How can I  
> > implement this to isLegalDate?
> 
> Use fromGregorianValid from package time.

but the problem is I shall implement it on my own in this exercise


> 
> Regards,
> Henk-Jan van Tuyl
> 
> 
> --
> http://Van.Tuyl.eu/
> http://members.chello.nl/hjgtuyl/tourdemonad.html
> --
> 
> 
> 
> -- 

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser


------------------------------

Message: 2
Date: Fri, 18 Dec 2009 15:57:56 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] find element of tupels
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

thx Daniel. Now I implemented it like that to use my monthAndMaxDay list and 
just use the isLeapYear check for February:

legalDate :: Date -> Bool
legalDate (myDay, Feb, myYear) = 0 < myDay && myDay <= (if isLeapYear        
    myYear then 29 else 28)
legalDate (myDay, myMonth, myYear) = maybe False id $ do
    days <- lookup myMonth monthAndMaxDay
    return (not (myDay <= 0) && (myDay <= days))


-------- Original-Nachricht --------
> Datum: Fri, 18 Dec 2009 00:17:56 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> Betreff: Re: [Haskell-beginners] find element of tupels

> Am Donnerstag 17 Dezember 2009 20:44:34 schrieb Henk-Jan van Tuyl:
> > On Thu, 17 Dec 2009 17:18:41 +0100, <[email protected]> wrote:
> > > thanks Magnus, great, works so far. I added to check that myDay have
> to
> > > be greater 0:
> > >
> > > legalDate :: Date -> Bool
> > > legalDate (myDay, myMonth, myYear) = maybe False id $ do
> > >     days <- lookup myMonth monthAndMaxDay
> > >     return (not (myDay <= 0) && (myDay <= days))
> > >
> > > Another thing is, that I have to check for leapYears. I have a
> function
> > > that checks if a year is a leap year
> > >
> > > isLeapYear :: Int -> Bool
> > > isLeapYear year = mod year 4 == 0
> >
> > That is not entirely correct; from package time:
> >    isLeapYear :: Integer -> Bool
> >    isLeapYear year = (mod year 4 == 0) && ((mod year 400 == 0) || not
> (mod
> > year 100 == 0))
> >
> > > monthAndMaxDay has 30 days for February which have to be fix. So i
> have
> > > to check in legalDate if the year is a leap year. If so I have to
> check
> > > myDay in February for 29 days, otherwise for 28 days. How can I
> > > implement this to isLegalDate?
> >
> > Use fromGregorianValid from package time.
> 
> Or
> 
> legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
> legalDate (d,m,y)
>     | m `elem` [Apr,Jun,Sep,Nov] = 0 < d && d < 31
>     | otherwise = 0 < d && d < 32
> 
> >
> > Regards,
> > Henk-Jan van Tuyl
> >
> >
> > --
> > http://Van.Tuyl.eu/
> > http://members.chello.nl/hjgtuyl/tourdemonad.html
> > --
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

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


------------------------------

Message: 3
Date: Fri, 18 Dec 2009 17:01:01 +0100
From: [email protected]
Subject: [Haskell-beginners] print all days of calendar
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

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


------------------------------

Message: 4
Date: Fri, 18 Dec 2009 18:06:32 +0100
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

2009/12/17  <[email protected]>:
> Hi,
> I have data Nat = Z | S Nat deriving (Eq,Ord,Show)
>
> toEnum should return Z for negative numbers. I did something like this but 
> the problem is I don't know how to set "less than". I tried > and lt:
>
> instance Enum Nat where
>    toEnum (gt 0) = S Z
>    toEnum (lt 1) = Z
> --
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>

Fill in the gaps:

instance Enum Nat where
    toEnum x | x <= 0 = ...
             | otherwise = ...

-- 
Deniz Dogan


------------------------------

Message: 5
Date: Fri, 18 Dec 2009 18:42:45 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

ahhh, ok, thanks so far. Here is how the output should looks like:
*Main> toEnum (-1) :: Nat
Z
*Main> toEnum 0 :: Nat 
Z
*Main> toEnum 1 :: Nat 
S Z 
*Main> fromEnum (S (S Z)) 
2

the first three things work, but I don't know about the last one. Does anybody 
how it should work and what's the sense of it?


-------- Original-Nachricht --------
> Datum: Fri, 18 Dec 2009 18:06:32 +0100
> Von: Deniz Dogan <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] Enum for natural numbers

> 2009/12/17  <[email protected]>:
> > Hi,
> > I have data Nat = Z | S Nat deriving (Eq,Ord,Show)
> >
> > toEnum should return Z for negative numbers. I did something like this
> but the problem is I don't know how to set "less than". I tried > and lt:
> >
> > instance Enum Nat where
> >    toEnum (gt 0) = S Z
> >    toEnum (lt 1) = Z
> > --
> > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> 
> Fill in the gaps:
> 
> instance Enum Nat where
>     toEnum x | x <= 0 = ...
>              | otherwise = ...
> 
> -- 
> Deniz Dogan

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser


------------------------------

Message: 6
Date: Fri, 18 Dec 2009 18:59:40 +0100
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

2009/12/18  <[email protected]>:
> ahhh, ok, thanks so far. Here is how the output should looks like:
> *Main> toEnum (-1) :: Nat
> Z
> *Main> toEnum 0 :: Nat
> Z
> *Main> toEnum 1 :: Nat
> S Z
> *Main> fromEnum (S (S Z))
> 2
>
> the first three things work, but I don't know about the last one. Does 
> anybody how it should work and what's the sense of it?
>

Looks right to me. "Z" means 0 (Zero), "S x" means "the Successor of
x". So (S (S (S Z))) means "the successor of the successor of the
successor of zero", which is 3.

>
> -------- Original-Nachricht --------
>> Datum: Fri, 18 Dec 2009 18:06:32 +0100
>> Von: Deniz Dogan <[email protected]>
>> An: [email protected]
>> CC: [email protected]
>> Betreff: Re: [Haskell-beginners] Enum for natural numbers
>
>> 2009/12/17  <[email protected]>:
>> > Hi,
>> > I have data Nat = Z | S Nat deriving (Eq,Ord,Show)
>> >
>> > toEnum should return Z for negative numbers. I did something like this
>> but the problem is I don't know how to set "less than". I tried > and lt:
>> >
>> > instance Enum Nat where
>> >    toEnum (gt 0) = S Z
>> >    toEnum (lt 1) = Z
>> > --
>> > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
>> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>> > _______________________________________________
>> > Beginners mailing list
>> > [email protected]
>> > http://www.haskell.org/mailman/listinfo/beginners
>> >
>>
>> Fill in the gaps:
>>
>> instance Enum Nat where
>>     toEnum x | x <= 0 = ...
>>              | otherwise = ...
>>
>> --
>> Deniz Dogan
>
> --
> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Deniz Dogan


------------------------------

Message: 7
Date: Fri, 18 Dec 2009 19:02:16 +0100
From: [email protected]
Subject: [Haskell-beginners] inserting tupels inside list as tupels in
        list
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

maybe the subject is a little bit confusing :p

I have a organiser list like that and have to add events (like panic, xmas, 
...) which is also a type of tupel for a specific date:

aliceOrg = [ ( (21,Dec,2009), [panic,aliceBDay] ) 
             ( (25,Dec,2009), [xmas] ) 
             ( (10,Jan,2010), [bobBDay] )

What function(s) can I use to add these nested lists and tupels here?

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser


------------------------------

Message: 8
Date: Fri, 18 Dec 2009 19:09:11 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] find element of tupels
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On 18/12/09 14:57, [email protected] wrote:
> thx Daniel. Now I implemented it like that to use my monthAndMaxDay list and 
> just use the isLeapYear check for February:
> 
> legalDate :: Date -> Bool
> legalDate (myDay, Feb, myYear) = 0 < myDay && myDay <= (if isLeapYear        
>     myYear then 29 else 28)
> legalDate (myDay, myMonth, myYear) = maybe False id $ do
>     days <- lookup myMonth monthAndMaxDay
>     return (not (myDay <= 0) && (myDay <= days))

I know it's a minor detail, but wouldn't it be clearer to change that last
line to read something like this:

  return $ 0 < myDay && myDay <= days

?

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20091218/1536c2ad/signature-0001.bin

------------------------------

Message: 9
Date: Fri, 18 Dec 2009 17:58:12 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] inserting tupels inside list as
        tupels in list
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Dec 18, 2009 at 07:02:16PM +0100, [email protected] wrote:
> maybe the subject is a little bit confusing :p
> 
> I have a organiser list like that and have to add events (like panic, xmas, 
> ...) which is also a type of tupel for a specific date:
> 
> aliceOrg = [ ( (21,Dec,2009), [panic,aliceBDay] ) 
>              ( (25,Dec,2009), [xmas] ) 
>              ( (10,Jan,2010), [bobBDay] )
> 
> What function(s) can I use to add these nested lists and tupels here?

You can use all the functions for manipulating lists and tuples in the
standard libraries... I'm not entirely sure what you are asking.

Let me suggest, however, that you probably don't want to be using
tuples so much; make your own data types.  Something like this:

  type Day = Int
  data Month = Jan | Feb | ...
  type Year = Int

  data Date = Date Day Month Year

  data CalEntry = CalEntry Date [Event]
  data Event = ...

  type Organizer = [CalEntry]

This is much less confusing since instead of just tuples everywhere
you have specific types that say exactly what the data represents.

-Brent


------------------------------

Message: 10
Date: Fri, 18 Dec 2009 15:55:34 -0800
From: David Gordon <[email protected]>
Subject: [Haskell-beginners] Fwd: Type constraints question
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Apologies for the spam, I had some difficulty getting my email address set
up right on the list and wanted to make sure this finally got through (or
perhaps there are like 5 copies on the list now... sorry.)

thanks

---------- Forwarded message ----------

Ok, now I will try asking the right question (previous try wasn't actually
an example of the problem I'm having)

Here's the code:


data Test = Test

data Test2 = Test2

class MyClass a where
    getChild :: MyClass b => a -> b

instance MyClass Test where
    getChild a = Test2

instance MyClass Test2 where
    getChild a = Test

On HUGS I get:

Error occurred
ERROR line 9 - Inferred type is not general enough
*** Expression    : getChild
*** Expected type : (MyClass Test, MyClass a) => Test -> a
*** Inferred type : (MyClass Test, MyClass Test2) => Test -> Test2

So, what's the problem with always returning a particular instance of
MyClass? I just want to constrain it to be an instance of MyClass, nothing
more.

thanks,

David


2009/12/18 David Gordon <[email protected]>

>
> Hi Folks,
> Total newbie here. I don't know if I am having a syntactic problem or a
conceptual problem.
> This code:
> data Test = Test
> data Test2 = Test2
> class MyClass a where
>     getChild :: (MyClass b) => a -> b
> instance MyClass Test where
>     getChild = Test2
> instance MyClass Test2 where
>     getChild = Test
> results in:
> [1 of 1] Compiling Main             ( test.hs, interpreted )
> test.hs:10:15:
>     Couldn't match expected type `Test -> b'
>            against inferred type `Test2'
>     In the expression: Test2
>     In the definition of `getChild': getChild = Test2
>     In the instance declaration for `MyClass Test'
> test.hs:13:15:
>     Couldn't match expected type `Test2 -> b'
>            against inferred type `Test'
>     In the expression: Test
>     In the definition of `getChild': getChild = Test
>     In the instance declaration for `MyClass Test2'
> Failed, modules loaded: none.
> Is this a reasonable thing to try and do in Haskell? If not, I have a lot
more questions... ;)
> many thanks,
> David
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091218/40110c22/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 18, Issue 15
*****************************************

Reply via email to