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 (Thomas)
2. Re: Compilation error (Daniel Seidel)
3. What book is being referred to: "... in SPJs beautiful code
chapter ..." (Costello, Roger L.)
4. Re: Compilation error (Thomas)
5. Re: What book is being referred to: "... in SPJs beautiful
code chapter ..." (Erlend Hamberg)
6. Re: Typeclasses vs. Data (Antoine Latter)
7. Re: What book is being referred to: "... in SPJs beautiful
code chapter ..." (Tom Murphy)
8. Re: What book is being referred to: "... in SPJs beautiful
code chapter ..." (Costello, Roger L.)
9. Re: Typeclasses vs. Data (Thomas)
----------------------------------------------------------------------
Message: 1
Date: Thu, 21 Jul 2011 12:26:15 +0200
From: Thomas <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Hi Felipe, David!
It works!!! :-)
What I do not really understand, however, is the difference between
eval_begin n k = eval n (if (n< 0) then k else (BeginCont k (n - 1)))
and
eval_begin n k = if (n< 0) then eval n k else eval n (BeginCont k (n
- 1))
Anyway, it works and since I needed to use the "Rank2Types" and
"RelaxedPolyRec" extensions I have to read (and hopefully understand ;-)
these as well. Maybe they'll shed some light onto this.
I just wonder: Are typeclasses such an advanced feature that I'd better
use alternatives wherever possible?
Yitzchak suggested this in this list a few days ago. But almost every
(introductory) text explains them as a basic feature.
Thanks a lot to both of you!
Thomas
On 21.07.2011 05:27, Felipe Almeida Lessa wrote:
> On Wed, Jul 20, 2011 at 8:41 PM, Thomas<[email protected]> wrote:
>> eval_begin :: Continuation a => Int -> a -> Int
>> eval_begin n k = eval n (if (n< 0) then k else (BeginCont k (n - 1)))
>
> Although 'eval n' is polymorphic, the if expression needs to have just
> one type, either 'a' or 'BeginCont a', and they can't be unified. The
> solution is pretty simple, though, since eval's return type doesn't
> mention 'a' at all:
>
> eval_begin :: Continuation a => Int -> a -> Int
> eval_begin n k = if (n< 0) then eval n k else eval n (BeginCont k (n - 1))
>
> Note that 'eval n' is repeated. If you don't to repeat it on your
> real world code you may give it an explicit name. However you'll need
> to provide a type signature from GHC 7.0 onwards:
>
> eval_begin :: Continuation a => Int -> a -> Int
> eval_begin n k =
> let eval' :: Continuation a => a -> Int
> eval' = eval n
> in if (n< 0) then eval' k else eval' (BeginCont k (n - 1))
>
> HTH, =)
>
------------------------------
Message: 2
Date: Thu, 21 Jul 2011 13:26:23 +0200
From: Daniel Seidel <[email protected]>
Subject: Re: [Haskell-beginners] Compilation error
To: mukesh tiwari <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain
Hi Mukesh,
it seems like in
>
> main = do
> let l = solve . convexHull . format . map ( map readInt .
> words ) . tail . lines
> printf "%.2f\n" l
> return ()
>
a String argument for l is missing, because
solve . convexHull . format . map ( map readInt . words ) . tail .
lines
has type String -> Double and for String -> Double there is no instance
to print it, that's what the error message tells.
Cheers,
Daniel.
>
------------------------------
Message: 3
Date: Thu, 21 Jul 2011 07:33:15 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] What book is being referred to: "... in
SPJs beautiful code chapter ..."
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hi Folks,
I saw this mention [1]:
I like the terminology in SPJs beautiful code chapter.
He talks about IO *actions* and how IO actions can
be glued together into larger IO actions, and that main
is then an IO action in itself.
Do you know what book is being referred to?
/Roger
[1] A comment by Sebastian Sylvan in:
http://book.realworldhaskell.org/read/io.html
------------------------------
Message: 4
Date: Thu, 21 Jul 2011 13:53:38 +0200
From: Thomas <[email protected]>
Subject: Re: [Haskell-beginners] Compilation error
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi Mukesh!
Mi guess is that you're simply not supplying any input to
solve . convexHull . format . map ( map readInt . words ) . tail . lines
So its type is a function which in turn can't be output via "printf".
Also I think your composition is not correct:
tail . lines "blabla" won't compile for example.
tail $ lines "bla bla" will.
But then on a one line input you'll get nothing...
HTH,
Thomas
On 21.07.2011 11:23, mukesh tiwari wrote:
> Kindly some one please tell me why this code in not compiling . I have to
> round a Double value up to two decimal places and i wrote this code for
> this problem [ http://www.spoj.pl/problems/QCJ4 ] .
> Thank you
> Mukesh Tiwari
>
> import Data.List
> import qualified Data.Sequence as DS
> import Text.Printf
>
> data Point a = P a a deriving ( Show , Eq , Ord )
> data Turn = S | L | R deriving ( Show , Eq , Ord , Enum ) -- straight left
> right
>
> compPoint :: ( Num a , Ord a ) => Point a -> Point a -> Ordering
> compPoint ( P x1 y1 ) ( P x2 y2 )
> | compare x1 x2 == EQ = compare y1 y2
> | otherwise = compare x1 x2
>
> findMinx :: ( Num a , Ord a ) => [ Point a ] -> [ Point a ]
> findMinx xs = sortBy ( \x y -> compPoint x y ) xs
>
> compAngle ::(Num a , Ord a ) => Point a -> Point a -> Point a -> Ordering
> compAngle ( P x1 y1 ) ( P x2 y2 ) ( P x0 y0 ) = compare ( ( y1 - y0 ) * (
> x2 - x0 ) ) ( ( y2 - y0) * ( x1 - x0 ) )
>
> sortByangle :: ( Num a , Ord a ) => [ Point a ] -> [ Point a ]
> sortByangle (z:xs) = z : sortBy ( \x y -> compAngle x y z ) xs
>
> convexHull ::( Num a , Ord a ) => [ Point a ] -> [ Point a ]
> convexHull xs = reverse . findHull [y,x] $ ys where
> (x:y:ys) = sortByangle.findMinx $ xs
>
> findTurn :: ( Num a , Ord a , Eq a ) => Point a -> Point a -> Point a ->
> Turn
> findTurn ( P x0 y0 ) ( P x1 y1 ) ( P x2 y2 )
> | ( y1 - y0 ) * ( x2- x0 )< ( y2 - y0 ) * ( x1 - x0 ) = L
> | ( y1 - y0 ) * ( x2- x0 ) == ( y2 - y0 ) * ( x1 - x0 ) = S
> | otherwise = R
>
> findHull :: ( Num a , Ord a ) => [ Point a ] -> [ Point a ] -> [ Point
> a
> ]
> findHull [x] ( z : ys ) = findHull [ z , x ] ys --incase of second point
> on line from x to z
> findHull xs [] = xs
> findHull ( y : x : xs ) ( z:ys )
> | findTurn x y z == R = findHull ( x : xs ) ( z:ys )
> | findTurn x y z == S = findHull ( x : xs ) ( z:ys )
> | otherwise = findHull ( z : y : x : xs ) ys
>
> --from here on testing part for SPOJ
>
> format::(Num a , Ord a ) => [[a]] -> [Point a]
> format xs = map (\[x0 , y0] -> P x0 y0 ) xs
>
> helpSqrt :: ( Floating a ) => Point a -> Point a -> a
> helpSqrt ( P x0 y0 ) ( P x1 y1 ) = sqrt $ ( x0 - x1 ) ^ 2 + ( y0 - y1 ) ^
> 2
>
> solve :: ( Num a , RealFrac a , Floating a ) => [ Point a ] -> a
> solve xs = d where
> d = snd . foldl ( \( P x0 y0 , s ) ( P x1 y1 ) -> ( P x0 y0 , max s
> $ 2.0 * helpSqrt ( P x0 y0 ) ( P x1 y1 ) ) ) ( P x y , 0 ) $ xs
> where
> ( P x y ) = cMass xs
>
>
> cMass :: ( Num a , RealFrac a , Floating a ) => [ Point a ] -> Point a
> cMass xs = P x y where
> ( P x0 y0 ) = foldl ( \( P x1 y1 ) (P x2 y2 ) -> P ( x1 + x2 ) ( y1 + y2 ) )
> ( P 0 0 ) xs
> n = genericLength xs
> x = x0 / n
> y = x0 / n
>
>
>
> readInt ::( Num a , Read a ) => String -> a
> readInt = read
>
>
> main = do
> let l = solve . convexHull . format . map ( map readInt . words )
> . tail . lines
> printf "%.2f\n" l
> return ()
>
> {--
> main = interact $ solve . convexHull . format . map ( map readInt . words )
> . tail . lines
> --}
>
> The error is
> [1 of 1] Compiling Main ( qcj4_6044.hs, qcj4_6044.o )
>
> qcj4_6044.hs:69:1:
> No instance for (PrintfArg (String -> a))
> arising from a use of `printf' at qcj4_6044.hs:69:1-18
> Possible fix:
> add an instance declaration for (PrintfArg (String -> a))
> In a stmt of a 'do' expression: printf "%.2f\n" l
> In the expression:
> do { let l = solve
> . convexHull . format . map (map readInt . words) .
> tail . lines;
> printf "%.2f\n" l;
> return () }
> In the definition of `main':
> main = do { let l = ...;
> printf "%.2f\n" l;
> return () }
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Thu, 21 Jul 2011 13:56:21 +0200
From: Erlend Hamberg <[email protected]>
Subject: Re: [Haskell-beginners] What book is being referred to: "...
in SPJs beautiful code chapter ..."
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<ca+g9oxkr-vrdf+s1n2261h35e-bn4c1e9tx4h9fb0lvefnu...@mail.gmail.com>
Content-Type: text/plain; charset="windows-1252"
On 21 Jul 2011 13:34, "Costello, Roger L." <[email protected]> wrote:
> Do you know what book is being referred to?
I'm quite sure it's ?Beautiful Code?:
http://oreilly.com/catalog/9780596510046
--
Erlend Hamberg
[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110721/32e2358b/attachment-0001.htm>
------------------------------
Message: 6
Date: Thu, 21 Jul 2011 07:04:54 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: Thomas <[email protected]>
Cc: [email protected]
Message-ID:
<cakjsnqe8taq0vwx2ifgespiufbyks91txouo6bxyzoovb32...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Jul 21, 2011 5:28 AM, "Thomas" <[email protected]> wrote:
>
> Hi Felipe, David!
>
> It works!!! :-)
>
> What I do not really understand, however, is the difference between
>
> eval_begin n k = eval n (if (n< 0) then k else (BeginCont k (n - 1)))
> and
>
> eval_begin n k = if (n< 0) then eval n k else eval n (BeginCont k (n -
1))
>
The difference comes down to two things:
1. The type of 'if'. The haskell 'if ... then ... else ...' is conceptually
just a function with the type (Bool -> a -> a -> a). The two branches must
be of the same type.
2. In the code:
> f (if p then a else b)
If somehow the 'if' could type-check with 'a' and 'b' being of different
types, the compiler wouldn't know which type to pick for 'f'. In Haskell,
we're not allowed to delay type-checking until run-time - the compiler
demands that it can solve everything before running any of it.
I hope I haven't confused things even more!
Antoine
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110721/358299c8/attachment-0001.htm>
------------------------------
Message: 7
Date: Thu, 21 Jul 2011 08:24:02 -0400
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] What book is being referred to: "...
in SPJs beautiful code chapter ..."
To: Erlend Hamberg <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<cao9q0twon-wxsd+qg1rbun4msbjhu8d_q+vknekl8kmxry7...@mail.gmail.com>
Content-Type: text/plain; charset=windows-1252
It is. You can find the chapter online here (although if you buy the
hard copy, proceeds go to Amnesty International!):
http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/index.htm#beautiful
Tom
On 7/21/11, Erlend Hamberg <[email protected]> wrote:
> On 21 Jul 2011 13:34, "Costello, Roger L." <[email protected]> wrote:
>> Do you know what book is being referred to?
>
> I'm quite sure it's ?Beautiful Code?:
> http://oreilly.com/catalog/9780596510046
>
> --
> Erlend Hamberg
> [email protected]
>
------------------------------
Message: 8
Date: Thu, 21 Jul 2011 08:33:10 -0400
From: "Costello, Roger L." <[email protected]>
Subject: Re: [Haskell-beginners] What book is being referred to: "...
in SPJs beautiful code chapter ..."
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
Ah, yes.
And SPJ = Simon Peyton Jones
Thanks Erlend and Tom!
/Roger
-----Original Message-----
From: Tom Murphy [mailto:[email protected]]
Sent: Thursday, July 21, 2011 8:24 AM
To: Erlend Hamberg
Cc: Costello, Roger L.; [email protected]
Subject: Re: [Haskell-beginners] What book is being referred to: "... in SPJs
beautiful code chapter ..."
It is. You can find the chapter online here (although if you buy the
hard copy, proceeds go to Amnesty International!):
http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/index.htm#beautiful
Tom
On 7/21/11, Erlend Hamberg <[email protected]> wrote:
> On 21 Jul 2011 13:34, "Costello, Roger L." <[email protected]> wrote:
>> Do you know what book is being referred to?
>
> I'm quite sure it's "Beautiful Code":
> http://oreilly.com/catalog/9780596510046
>
> --
> Erlend Hamberg
> [email protected]
>
------------------------------
Message: 9
Date: Thu, 21 Jul 2011 14:58:56 +0200
From: Thomas <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses vs. Data
To: Antoine Latter <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Hello Antoine!
Thank you for the explanation.
I think I get closer with it.
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.
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.')
Probably I do not yet understand well the relationship between types and
type classes...
Regards,
Thomas
On 21.07.2011 14:04, Antoine Latter wrote:
[...]
>
> The difference comes down to two things:
>
> 1. The type of 'if'. The haskell 'if ... then ... else ...' is conceptually
> just a function with the type (Bool -> a -> a -> a). The two branches must
> be of the same type.
>
> 2. In the code:
>
>> f (if p then a else b)
>
> If somehow the 'if' could type-check with 'a' and 'b' being of different
> types, the compiler wouldn't know which type to pick for 'f'. In Haskell,
> we're not allowed to delay type-checking until run-time - the compiler
> demands that it can solve everything before running any of it.
>
> I hope I haven't confused things even more!
>
> Antoine
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 42
*****************************************