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. Type constraints question (David Gordon)
2. Re: Type constraints question (David Gordon)
3. Re: Pure Regex.PCRE Possible? (PLEAC, Fix Style) (Chadda? Fouch?)
4. Re: Enum for natural numbers ([email protected])
5. Re: Enum for natural numbers (Deniz Dogan)
6. Re: inserting tupels inside list as tupels in list
([email protected])
----------------------------------------------------------------------
Message: 1
Date: Fri, 18 Dec 2009 02:54:03 -0800
From: David Gordon <[email protected]>
Subject: [Haskell-beginners] Type constraints question
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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/ef193dce/attachment-0001.html
------------------------------
Message: 2
Date: Fri, 18 Dec 2009 15:19:05 -0800
From: David Gordon <[email protected]>
Subject: [Haskell-beginners] Re: Type constraints question
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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/2b3d0fef/attachment-0001.html
------------------------------
Message: 3
Date: Sun, 20 Dec 2009 10:16:59 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Pure Regex.PCRE Possible? (PLEAC, Fix
Style)
To: Volkan YAZICI <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, Dec 19, 2009 at 2:37 PM, Volkan YAZICI <[email protected]> wrote:
> However, if I'm not mistaken, above functions _should_ be absolutely
> pure. Is it possible to encapsulate impure PCRE functions into their
> pure equivalents?
>
> BTW, why are PCRE functions impure?
Mainly because they _are_ the core that _is_ encapsulated as you
suggested : regex-pcre as many other regex library, provides the same
basic mostly pure interface which is in regex-base.
That interface comes with many specific pure function (matchText,
matchOnce, matchAll and so on, as well as makeRegex and others) and
some "magic" operators like =~ which decides what kind of matching you
want to do depending on the type of the result.
See this tutorial for some example of usage :
http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutorial/
--
Jedaï
------------------------------
Message: 4
Date: Sun, 20 Dec 2009 14:36:06 +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"
Maybe I didn't understand the exercise if have to do. It says:
"Write the instance Enum Nat where toEnum is defined as a total function that
returns Z for negative integers. Some examples:
*Main> toEnum (-1) :: Nat
Z
*Main> toEnum 0 :: Nat
Z
*Main> toEnum 1 :: Nat
S Z
*Main> fromEnum (S (S Z))
2
so I did:
data Nat = Z | S Nat deriving (Eq,Ord,Show)
instance Enum Nat where
toEnum x|x > 0 = S Z
|otherwise = Z
somehow it looks really wrong. Do you understand want I have to do and how it
should look like?
-------- Original-Nachricht --------
> Datum: Fri, 18 Dec 2009 18:59:40 +0100
> Von: Deniz Dogan <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] Enum for natural numbers
> 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
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
------------------------------
Message: 5
Date: Sun, 20 Dec 2009 14:45:02 +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/20 <[email protected]>:
> Maybe I didn't understand the exercise if have to do. It says:
> "Write the instance Enum Nat where toEnum is defined as a total function that
> returns Z for negative integers. Some examples:
> *Main> toEnum (-1) :: Nat
> Z
> *Main> toEnum 0 :: Nat
> Z
> *Main> toEnum 1 :: Nat
> S Z
> *Main> fromEnum (S (S Z))
> 2
>
> so I did:
> data Nat = Z | S Nat deriving (Eq,Ord,Show)
> instance Enum Nat where
> toEnum x|x > 0 = S Z
> |otherwise = Z
>
>
> somehow it looks really wrong. Do you understand want I have to do and how it
> should look like?
>
In your current definition of toEnum, you always return "S Z" (in
other words, "1", "the successor of zero") for any integer i > 0. This
is clearly incorrect as you said. Your "otherwise" guard is correct,
but the other one is not. I get the feeling that this exercise is
about learning how to use recursion, so look into that.
A few relevant links:
http://en.wikibooks.org/wiki/Haskell/Recursion (the factorial example)
http://learnyouahaskell.com/recursion
http://book.realworldhaskell.org/read/functional-programming.html
Hope that helps
--
Deniz Dogan
------------------------------
Message: 6
Date: Sun, 20 Dec 2009 14:50:41 +0100
From: [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="iso-8859-1"
I already have things like that:
type Day = Int 17 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)
type ID = Int
type Event = (ID,String)
type Organiser = [ (Date, [Event]) ]
and now I should implement a function like this:
addEvents :: Date -> [Event] -> Organiser -> Organiser
that adds to an organiser a list of events for a specific date
an example looks like this:
xmas :: Event
xmas = (2, "Christmas")
aliceOrg :: Organiser
aliceOrg = [ ( (21,Dec,2009), [panic,aliceBDay] ),
( (25,Dec,2009), [xmas] ) ]
-------- Original-Nachricht --------
> Datum: Fri, 18 Dec 2009 17:58:12 -0500
> Von: Brent Yorgey <[email protected]>
> An: [email protected]
> Betreff: Re: [Haskell-beginners] inserting tupels inside list as tupels in
> list
> 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
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
--
Preisknaller: GMX DSL Flatrate für nur 16,99 Euro/mtl.!
http://portal.gmx.net/de/go/dsl02
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 17
*****************************************