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: Typeclasses vs. Data (Brent Yorgey)
2. Re: Typeclasses vs. Data (Felipe Almeida Lessa)
3. Re: Typeclasses vs. Data (David Place)
4. Re: Drawing Information from a function already defined
(Clockwork PC)
5. Re: Typeclasses vs. Data (Thomas)
6. Re: Typeclasses vs. Data (Felipe Almeida Lessa)
7. another list comprehesion error (Roelof Wobben)
----------------------------------------------------------------------
Message: 1
Date: Thu, 21 Jul 2011 09:08:10 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Jul 21, 2011 at 12:26:15PM +0200, Thomas wrote:
>
> I just wonder: Are typeclasses such an advanced feature that I'd
> better use alternatives wherever possible?
No. Typeclasses are a (relatively) straightforward, and quite useful,
feature. Yitzchak was arguing that type classes are often overused by
beginners, for purposes where other, better solutions exist. If you
find yourself using type classes all over the place for everything
then perhaps you are using them too much. But there is no need to
avoid them completely, and certainly not because they are "too
advanced".
The obvious question you may be wondering is "well, when should I use
them and when shouldn't I?" Unfortunately I can't answer that off the
top of my head. Just read and write a lot of code, try out different
ways of solving problems, and you'll start to get a feel for what
works well and what doesn't.
-Brent
------------------------------
Message: 2
Date: Thu, 21 Jul 2011 10:09:36 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: Thomas <[email protected]>
Cc: [email protected]
Message-ID:
<CANd=ogeqvvkre20-__lao7nvqeekvakaxb3g997av5dc9yy...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Thu, Jul 21, 2011 at 9:58 AM, Thomas <[email protected]> wrote:
> So my understanding is this: In my example both 'a' and 'b' were of the same
> typeclass. In fact they were of the same type, too, since in the example the
> class had only one instance. However, the compiler/type checker could not
> prove this to be true.
Actually they had different types and the compiler could see it :).
Let's see that if again:
if n < 0 then k else BeginCont k (n - 1)
Let's say that 'k' has type X. What is the type of 'BeginCont k (n -
1)'? Well, we have
data BeginCont a = BeginCont a Int
so given that the first parameter of the 'BeginCont' is 'k', which has
type 'X', then 'BeginCont k (n-1)' has type 'BeginCont X'.
So this is what we have:
if n < 0
then (k :: X)
else (BeginCont k (n-1) :: BeginCont X)
What is the type of the whole 'if'? It must be the unification of 'X'
and 'BeginCont X'. But we can't unify these types. It doesn't really
matter that both 'X' and 'BeginCont X' are instances of some typeclass
MyTC, and that the function that will take the result of the 'if' just
needs the typeclass and nothing else. We can't typecheck the 'if'.
> But if this was the case then I should be able to convince the compiler via
> type annotations like
> ?if p then (a :: (MyTC a) => a) else (b :: (MyTC a) => a)
> which does not work ('Inferred type is less polymorphic then expected.')
It is less polymorphic because 'k' has a rigid, defined type. Its
type was chosen by the one who called the function. He gave you some
'k' that satisfy the class constraint, but you don't know which one.
HTH, =)
--
Felipe.
------------------------------
Message: 3
Date: Thu, 21 Jul 2011 09:24:34 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: Felipe Almeida Lessa <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jul 21, 2011, at 9:09 AM, Felipe Almeida Lessa wrote:
> It is less polymorphic because 'k' has a rigid, defined type. Its
> type was chosen by the one who called the function. He gave you some
> 'k' that satisfy the class constraint, but you don't know which one.
I think that Thomas made an interesting step in his first post that may explain
the problem. First, he created a type that had (or would eventually have had)
constructors for each of the different kinds of continuations. This type is
proper type in the mind of the type inference algorithm. Then, in the interest
of modularity, he substituted a class for the type. The class is not a proper
type, so the substitution doesn't work.
I remember going through this same confusion coming from a more object-oriented
way of thinking. A class in Haskell is more like an interface or a protocol
in object-oriented languages. I think of it as an orthogonal construct to
type.
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
------------------------------
Message: 4
Date: Thu, 21 Jul 2011 23:53:37 +1000
From: Clockwork PC <[email protected]>
Subject: Re: [Haskell-beginners] Drawing Information from a function
already defined
To: john melesky <[email protected]>
Cc: [email protected]
Message-ID:
<cal1-yvpcc6dxkcl5em_lp4hawp8jiofyioh5dcwgvar9vpv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks to everyone for such a response! I'm overwhelmed.
Your suggestions were very helpful and I'd like to share my first success in
Haskell with you...
I wrote a little file triangle.hs that contains this:
rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10], a^2 +
b^2 == c^2 ]
And then loaded it in GHCi:
*GHCi, version 7.0.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l triangle
[1 of 1] Compiling Main ( triangle.hs, interpreted )
Ok, modules loaded: Main.
*Main>
*
Then I tried the first approach:
*Main> let myTriangle = [ (a,b,c) | (a,b,c) <- rightTriangles, a+b+c == 24 ]
*Main> myTriangle
[(8,6,10),(6,8,10)]
I also tried the second simple approach:
*Main> let myTriangles2 = filter sum24 rightTriangles where sum24 (a,b,c) =
(a+b+c) == 24
*Main> myTriangles2
[(8,6,10),(6,8,10)]
Hooray! Thank you, John and Brent.
Thank you also to Felipe for your correction.
And finally, think you to Aditya. I don't really understand Monads yet, but
when I get to that chapter in my guide I'll refer to your email many times,
I'm sure.
Thanks again to all you lovely people for helping me with my query :-)
Alexander
On Wed, Jul 20, 2011 at 23:57, john melesky <[email protected]> wrote:
> On Wed, Jul 20, 2011 at 11:45:52PM +1000, Clockwork PC wrote:
> > Defined my function:
> >
> > Prelude> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a
> <-
> > [1..10], a^2 + b^2 == c^2 ]
> >
> > ...
> >
> > Now, I want to define a refinement of this that will only select values
> > whose total is 24.
> >
> > ...
> >
> > Basically, I'm trying to work out how to draw data from a list already to
> > hand.
>
> This last part says it. You already know how to draw data from a list:
> you do it above when you pull, e.g., c from [1..10]. You actually do
> it three times (for a, b, and c).
>
> This time around, you have an existing list (named rightTriangles),
> and you want to pull (a,b,c) from it, rather than pulling a, b, and c
> from separate lists. Otherwise, it's much the same as what you were
> doing before.
>
> So the structure of it would look something like:
>
> let myTriangles = [ (a,b,c) | (a,b,c) <- rightTriangles, *condition* ]
> ^ a new name ^ same sort of output ^ from your existing list
>
> where *condition* is your "add up to 24" requirement, in the same form
> as the "a^2+b^2==c^2" requirement in your first list comprehension.
>
> Hope that helps.
>
> -john
>
>
> _______________________________________________
> 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/20110721/0bff3f9c/attachment-0001.htm>
------------------------------
Message: 5
Date: Thu, 21 Jul 2011 17:22:45 +0200
From: Thomas <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hello!
I think the pieces all start to fall into place. :-)
I still haven't truly understood typeclasses, but at least I see
progress in my understanding.
The analogy between OO-interfaces and typeclasses seems to be a bit
misleading here, though. Because IIUC then in Haskell typeclasses are
not a substitute for a type - a misconception that bit me here, while in
(at least the OO-languages I use) interfaces can usually be used instead
of types (except object creation & assignment).
For example:
if n < 0
then (k :: X)
else (BeginCont k (n-1) :: BeginCont X)
does not type check in Haskell.
The "equivalent" construct would type check in OO(*) - although I
understand that this does not mean the same in OO as in Haskell.
Probably type classes are really orthogonal to types as David writes.
But then I would consider them as rather different from interfaces in
OO, too.
Well, the Haskell road is still long for me, but it's real fun
travelling... ;-)
Thank you, everybody!
Thomas
PS:
*) class BeginCont : public InterfaceX
...
if(n < 0) {
k; // type is: InterfaceX*
} else {
new BeginCont(k, (n - 1)); // type is: BeginCont* ~= InterfaceX*
}
Ok, the type check is trivial (essentially a NOOP) in the code above,
but it would even type check as:
CallWithInterfaceX((n<0)?k:new BeginCont(k, n-1));
which is rather close to the Haskell code semantically.
On 21.07.2011 15:24, David Place wrote:
> On Jul 21, 2011, at 9:09 AM, Felipe Almeida Lessa wrote:
>
>> It is less polymorphic because 'k' has a rigid, defined type. Its
>> type was chosen by the one who called the function. He gave you some
>> 'k' that satisfy the class constraint, but you don't know which one.
>
> I think that Thomas made an interesting step in his first post that may
> explain the problem. First, he created a type that had (or would eventually
> have had) constructors for each of the different kinds of continuations.
> This type is proper type in the mind of the type inference algorithm. Then,
> in the interest of modularity, he substituted a class for the type. The
> class is not a proper type, so the substitution doesn't work.
>
> I remember going through this same confusion coming from a more
> object-oriented way of thinking. A class in Haskell is more like an
> interface or a protocol in object-oriented languages. I think of it as an
> orthogonal construct to type.
>
> ____________________
> David Place
> Owner, Panpipes Ho! LLC
> http://panpipesho.com
> [email protected]
>
>
------------------------------
Message: 6
Date: Thu, 21 Jul 2011 12:37:03 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: Thomas <[email protected]>
Cc: [email protected]
Message-ID:
<CANd=OGG6xJLpa7P5=+dezwohkjd-3c1knqt-radc422jy6l...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Thu, Jul 21, 2011 at 12:22 PM, Thomas <[email protected]> wrote:
> The analogy between OO-interfaces and typeclasses seems to be a bit
> misleading here, though. Because IIUC then in Haskell typeclasses are not a
> substitute for a type - a misconception that bit me here, while in (at least
> the OO-languages I use) interfaces can usually be used instead of types
> (except object creation & assignment).
I prefer to say that the only similarity between OO classes and
typeclasses is the word "class". Trying to make comparisons always
ends in tears.
> For example:
> ? ?if n < 0
> ? ? ? ?then (k :: X)
> ? ? ? ?else (BeginCont k (n-1) :: BeginCont X)
> does not type check in Haskell.
> The "equivalent" construct would type check in OO(*) - although I understand
> that this does not mean the same in OO as in Haskell.
It is possible to do the same in Haskell using existentials. But
forget that I've said that and don't try to use it. Most of the time
you don't need them and there are better solutions.
Cheers, =)
--
Felipe.
------------------------------
Message: 7
Date: Thu, 21 Jul 2011 17:27:20 +0000
From: Roelof Wobben <[email protected]>
Subject: [Haskell-beginners] another list comprehesion error
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
hello,
I know try to make this work.
The user enters a number and the programm calculates all the numbers which are
smaller then the number and where x^2+Y^2=Z^2.
So i Thought this would work.
roelof :: a -> b -> c -> (a,b,c)
roelof n = [(x y z) | x^2+Y^2=Z^2 <- x<-[1..n], y<- [1..n], z<-[1..n]]
But I get this error : oefening.hs:2:30: parse error on input `='
Roelof
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110721/02a8d86d/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 43
*****************************************