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: safetail problem (David Place)
2. Problems with typeclasses instances (ARJANEN Lo?c Jean David)
3. Re: Problems with typeclasses instances (David McBride)
4. Re: Problems with typeclasses instances (Antoine Latter)
5. Re: safetail problem (Roelof Wobben)
6. Re: safetail problem (Roelof Wobben)
7. Re: safetail problem (Kyle Murphy)
----------------------------------------------------------------------
Message: 1
Date: Wed, 13 Jul 2011 08:10:38 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: Roelof Wobben <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jul 13, 2011, at 3:07 AM, Roelof Wobben wrote:
> changed it to this : http://codepad.org/ROV4ASAB
Hi, Roelof.
Have you installed GHC? You will find the error messages to be more helpful
when you use ghci to execute your code.
I don't know Programming in Haskell by Graham Hutton. I am sure that it very
good, but maybe not the best for you. You might also try this tutorial.
> http://learnyouahaskell.com/chapters
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
------------------------------
Message: 2
Date: Wed, 13 Jul 2011 14:56:33 +0200
From: ARJANEN Lo?c Jean David <[email protected]>
Subject: [Haskell-beginners] Problems with typeclasses instances
To: [email protected]
Message-ID:
<CAB2q81a1qXRy0vK+6i8+87rJ=X0rcDtUSY+m6XQA+BW=zm0...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I have the following problem with typeclasses : I have created a type
class in a manner similar to the following
module Dummy where
import Char
class (Show t) => Dummy t where
kite :: t -> t
instance (Dummy t) => Dummy [t] where
kite = reverse
instance Dummy String where
kite = map toUpper
And use it thus :
import Dummy
main :: IO ()
main = do
print.kite $ "Rest in peace"
If I compile without any option, I get as expected an error because of
the type synonym instance. If I remove the instance for String, I also
get as expected an error because Char is not an instance of Dummy. But
if I add (as suggested in the first error message) the GHC's option
-XTypeSynonymInstances, I get an error because of overlapping
instances, even if Char is still not an instance of Dummy and I don't
see how String could be eligible for the (Dummy t) => Dummy [t]
instance...
Thanks in advance for your help,
ARJANEN Loic.
------------------------------
Message: 3
Date: Wed, 13 Jul 2011 09:10:21 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Problems with typeclasses instances
To: ARJANEN Lo?c Jean David <[email protected]>
Cc: [email protected]
Message-ID:
<CAN+Tr41ZQ9F=Zwhz=N4k8fOba7B0ayfnjkFsmFnRRJc=lcy...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
You can add OverlappingInstances to allow for both [a] and [Char]
intstances be used. It will choose the least general instance for
you.
On Wed, Jul 13, 2011 at 8:56 AM, ARJANEN Lo?c Jean David
<[email protected]> wrote:
> I have the following problem with typeclasses : I have created a type
> class in a manner similar to the following
>
> module Dummy where
> import Char
> class (Show t) => Dummy t where
> ? ?kite :: t -> t
> instance (Dummy t) => Dummy [t] where
> ? ?kite = reverse
> instance Dummy String where
> ? ?kite = map toUpper
>
> And use it thus :
>
> import Dummy
> main :: IO ()
> main = do
> ? ?print.kite $ "Rest in peace"
>
> If I compile without any option, I get as expected an error because of
> the type synonym instance. If I remove the instance for String, I also
> get as expected an error because Char is not an instance of Dummy. But
> if I add (as suggested in the first error message) the GHC's option
> -XTypeSynonymInstances, I get an error because of overlapping
> instances, even if Char is still not an instance of Dummy and I don't
> see how String could be eligible for the (Dummy t) => Dummy [t]
> instance...
>
> Thanks in advance for your help,
> ARJANEN Loic.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Wed, 13 Jul 2011 09:01:39 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Problems with typeclasses instances
To: ARJANEN Lo?c Jean David <[email protected]>
Cc: [email protected]
Message-ID:
<CAKjSnQHGYUT5exq38uc33qq+D2hryBgh_gSdJA8a=iv7oqk...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Wed, Jul 13, 2011 at 7:56 AM, ARJANEN Lo?c Jean David
<[email protected]> wrote:
>
> If I compile without any option, I get as expected an error because of
> the type synonym instance. If I remove the instance for String, I also
> get as expected an error because Char is not an instance of Dummy. But
> if I add (as suggested in the first error message) the GHC's option
> -XTypeSynonymInstances, I get an error because of overlapping
> instances, even if Char is still not an instance of Dummy and I don't
> see how String could be eligible for the (Dummy t) => Dummy [t]
> instance...
>
This is one of the trickier bits about Haskell typeclasses. The instance:
> instance (Dummy t) => Dummy [t]
can be read in English as:
"The type [t] is an instance of Dummy. Also, using this instance is a
compilation failure if t is not an instance of Dummy."
More succinctly, the information to the left of the (=>) is not used
at all in instance resolution. It does, however, bring the constraint
into scope in the body of the instance declaration, so it isn't
useless.
Antoine
> Thanks in advance for your help,
> ARJANEN Loic.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Wed, 13 Jul 2011 14:54:01 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello,
I have GHC installed so I will try this one with ghci.
Roelof
----------------------------------------
> Subject: Re: [Haskell-beginners] safetail problem
> From: [email protected]
> Date: Wed, 13 Jul 2011 08:10:38 -0400
> CC: [email protected]
> To: [email protected]
>
> On Jul 13, 2011, at 3:07 AM, Roelof Wobben wrote:
>
> > changed it to this : http://codepad.org/ROV4ASAB
>
>
> Hi, Roelof.
>
> Have you installed GHC? You will find the error messages to be more helpful
> when you use ghci to execute your code.
>
> I don't know Programming in Haskell by Graham Hutton. I am sure that it very
> good, but maybe not the best for you. You might also try this tutorial.
>
> > http://learnyouahaskell.com/chapters
>
> ____________________
> David Place
> Owner, Panpipes Ho! LLC
> http://panpipesho.com
> [email protected]
>
>
>
------------------------------
Message: 6
Date: Wed, 13 Jul 2011 14:58:03 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
With GHCI I get this message :
safetail.hs:1:16: parse error on input ','
Roelof
----------------------------------------
> From: [email protected]
> To: [email protected]
> Date: Wed, 13 Jul 2011 14:54:01 +0000
> Subject: Re: [Haskell-beginners] safetail problem
>
>
> Hello,
>
>
>
> I have GHC installed so I will try this one with ghci.
>
>
>
> Roelof
>
>
>
> ----------------------------------------
> > Subject: Re: [Haskell-beginners] safetail problem
> > From: [email protected]
> > Date: Wed, 13 Jul 2011 08:10:38 -0400
> > CC: [email protected]
> > To: [email protected]
> >
> > On Jul 13, 2011, at 3:07 AM, Roelof Wobben wrote:
> >
> > > changed it to this : http://codepad.org/ROV4ASAB
> >
> >
> > Hi, Roelof.
> >
> > Have you installed GHC? You will find the error messages to be more helpful
> > when you use ghci to execute your code.
> >
> > I don't know Programming in Haskell by Graham Hutton. I am sure that it
> > very good, but maybe not the best for you. You might also try this tutorial.
> >
> > > http://learnyouahaskell.com/chapters
> >
> > ____________________
> > David Place
> > Owner, Panpipes Ho! LLC
> > http://panpipesho.com
> > [email protected]
> >
> >
> >
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 7
Date: Wed, 13 Jul 2011 11:40:51 -0400
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: Roelof Wobben <[email protected]>
Cc: [email protected]
Message-ID:
<CA+y6JcyDKf7wAdmQ=3mchgmh_pcwm92fakc-mgdfyvtvumw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Your new code declares that you're expecting a [x,xs] which isn't a valid
type. What you're expecting is a [x]. Furthermore you then try to pattern
match against (x,xs) which is a tuple even though your function just
declared it's expecting a list. What you want is:
safetail :: [x] -> [x]
safetail (x:xs) = if null[xs] then [] else xs
(x:xs) is a pattern match using one of the two list constructors (:) which
has type:
(:) :: a -> [a] -> [a]
This means that (:) takes two arguments, anything, and a list of anything,
and returns a new list of anything (in this case with the first argument
pre-pended to the second argument).
The other list constructor ([]) is a zero argument constructor of type:
[] :: [a]
That is, it can be used to construct an empty list of any type.
With these two constructors it's possible to understand how lists working in
Haskell.
[1,2,3,4] is syntactic sugar (that is, it's replaced by the compiler for you
and exists solely for convenience of writing) for 1:2:3:4:[].
Following the types you have:
[] -> Constructs an empty list of type [a], we don't know what "a" is yet.
4:[] -> Takes an Int (4) and a list [Int] ([]) and constructs a new list of
type [Int]. The previous type of [a] is forced to be [Int] at this point.
3:[4] -> Takes an Int (3) and a list ([4]) and returns a new list ([3,4]).
2:[3,4] -> Takes an Int (2) and a list ([3,4]) and returns a new list
([2,3,4]).
1:[2,3,4] -> Takes an Int (1) and a list ([2,3,4]) and returns a new list
([1,2,3,4]).
N.B. I'm not an expert on Haskell, so the following might be wrong in some
of the details, but is the way I understand lists to function in Haskell.
Don't be confused at this point with the empty list constructor [], and the
list type ([]), which even though they use the same name are actually two
different things.
To be clear, the data declaration of List actually looks similar to the
following:
data ([]) a = [] | (:) a (([]) a)
which could also be written as:
data [a] = [] | a:[a]
The (([]) a) on the left of the equal sign is the Type. The [] on the right
of the equal sign is the constructor.
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Wed, Jul 13, 2011 at 10:58, Roelof Wobben <[email protected]> wrote:
>
> With GHCI I get this message :
>
>
>
> safetail.hs:1:16: parse error on input ','
>
>
>
> Roelof
>
>
>
> ----------------------------------------
> > From: [email protected]
> > To: [email protected]
> > Date: Wed, 13 Jul 2011 14:54:01 +0000
> > Subject: Re: [Haskell-beginners] safetail problem
> >
> >
> > Hello,
> >
> >
> >
> > I have GHC installed so I will try this one with ghci.
> >
> >
> >
> > Roelof
> >
> >
> >
> > ----------------------------------------
> > > Subject: Re: [Haskell-beginners] safetail problem
> > > From: [email protected]
> > > Date: Wed, 13 Jul 2011 08:10:38 -0400
> > > CC: [email protected]
> > > To: [email protected]
> > >
> > > On Jul 13, 2011, at 3:07 AM, Roelof Wobben wrote:
> > >
> > > > changed it to this : http://codepad.org/ROV4ASAB
> > >
> > >
> > > Hi, Roelof.
> > >
> > > Have you installed GHC? You will find the error messages to be more
> helpful when you use ghci to execute your code.
> > >
> > > I don't know Programming in Haskell by Graham Hutton. I am sure that it
> very good, but maybe not the best for you. You might also try this tutorial.
> > >
> > > > http://learnyouahaskell.com/chapters
> > >
> > > ____________________
> > > David Place
> > > Owner, Panpipes Ho! LLC
> > > http://panpipesho.com
> > > [email protected]
> > >
> > >
> > >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110713/bac0ea48/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 24
*****************************************