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 [ <a href="http://www.spoj.pl/problems/QCJ4">http://www.spoj.pl/problems/QCJ4</a> ] . <div>
Thank you</div><div>Mukesh Tiwari</div><div><br></div><div><div>import Data.List</div><div>import qualified Data.Sequence as DS</div><div>import Text.Printf </div><div><br></div><div>data Point a = P a a deriving ( Show , Eq , Ord )</div>
<div>data Turn = S | L | R deriving ( Show , Eq , Ord , Enum ) -- straight left right </div><div><br></div><div>compPoint :: ( Num a , Ord a ) => Point a -> Point a -> Ordering</div><div>compPoint ( P x1 y1 ) ( P x2 y2 )</div>
<div> | compare x1 x2 == EQ = compare y1 y2</div><div> | otherwise = compare x1 x2 </div><div><br></div><div>findMinx :: ( Num a , Ord a ) => [ Point a ] -> [ Point a ]</div><div>findMinx xs = sortBy ( \x y -> compPoint x y ) xs</div>
<div><br></div><div>compAngle ::(Num a , Ord a ) => Point a -> Point a -> Point a -> Ordering</div><div>compAngle ( P x1 y1 ) ( P x2 y2 ) ( P x0 y0 ) = compare ( ( y1 - y0 ) * ( x2 - x0 ) ) ( ( y2 - y0) * ( x1 - x0 ) )</div>
<div><br></div><div>sortByangle :: ( Num a , Ord a ) => [ Point a ] -> [ Point a ]</div><div>sortByangle (z:xs) = z : sortBy ( \x y -> compAngle x y z ) xs </div><div><br></div><div>convexHull ::( Num a , Ord a )<span class="Apple-tab-span" style="white-space:pre">        </span>=> [ Point a ] -> [ Point a ]</div>
<div>convexHull xs = reverse . findHull [y,x] $ ys where</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>(x:y:ys) = sortByangle.findMinx $ xs </div><div><br></div><div>findTurn :: ( Num a , Ord a , Eq a ) => Point a -> Point a -> Point a -> Turn</div>
<div>findTurn ( P x0 y0 ) ( P x1 y1 ) ( P x2 y2 )</div><div> | ( y1 - y0 ) * ( x2- x0 ) < ( y2 - y0 ) * ( x1 - x0 ) = L</div><div> | ( y1 - y0 ) * ( x2- x0 ) == ( y2 - y0 ) * ( x1 - x0 ) = S</div><div> | otherwise = R </div>
<div><br></div><div>findHull :: ( Num a , Ord a ) => [ Point a ] -> [ Point a ] -> [ Point a ]</div><div>findHull [x] ( z : ys ) = findHull [ z , x ] ys --incase of second point on line from x to z</div>
<div>findHull xs [] = xs</div><div>findHull ( y : x : xs ) ( z:ys )</div><div> | findTurn x y z == R = findHull ( x : xs ) ( z:ys )</div><div> | findTurn x y z == S = findHull ( x : xs ) ( z:ys )</div><div> | otherwise = findHull ( z : y : x : xs ) ys</div>
<div><br></div><div>--from here on testing part for SPOJ </div><div><br></div><div>format::(Num a , Ord a ) => [[a]] -> [Point a]</div><div>format xs = map (\[x0 , y0] -> P x0 y0 ) xs </div><div><br></div><div>helpSqrt :: ( Floating a ) => Point a -> Point a -> a</div>
<div>helpSqrt ( P x0 y0 ) ( P x1 y1 ) = sqrt $ ( x0 - x1 ) ^ 2 + ( y0 - y1 ) ^ 2 </div><div><br></div><div>solve :: ( Num a , RealFrac a , Floating a ) => [ Point a ] -> a </div><div>solve xs = d where</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> 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 </div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>( P x y ) = cMass xs </div><div><br></div><div><br></div><div>cMass :: ( Num a , RealFrac a , Floating a ) => [ Point a ] -> Point a </div><div>cMass xs = P x y where </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>( P x0 y0 ) = foldl ( \( P x1 y1 ) (P x2 y2 ) -> P ( x1 + x2 ) ( y1 + y2 ) ) ( P 0 0 ) xs </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>n = genericLength xs </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>x = x0 / n </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>y = x0 / n </div><div><br></div><div><br></div><div><br></div><div>readInt ::( Num a , Read a ) => String -> a</div>
<div>readInt = read</div><div><br></div><div><br></div><div>main = do </div><div> let l = solve . convexHull . format . map ( map readInt . words ) . tail . lines</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf "%.2f\n" l </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>return () </div><div><br></div><div>{--</div><div>main = interact $ solve . convexHull . format . map ( map readInt . words ) . tail . lines </div><div>--}</div>
<div><br></div><div>The error is </div><div><div>[1 of 1] Compiling Main ( qcj4_6044.hs, qcj4_6044.o )</div><div><br></div><div>qcj4_6044.hs:69:1:</div><div> No instance for (PrintfArg (String -> a))</div>
<div> arising from a use of `printf' at qcj4_6044.hs:69:1-18</div><div> Possible fix:</div><div> add an instance declaration for (PrintfArg (String -> a))</div><div> In a stmt of a 'do' expression: printf "%.2f\n" l</div>
<div> In the expression:</div><div> do { let l = solve</div><div> . convexHull . format . map (map readInt . words) . tail . lines;</div><div> printf "%.2f\n" l;</div><div>
return () }</div><div> In the definition of `main':</div><div> main = do { let l = ...;</div><div> printf "%.2f\n" l;</div><div> return () }</div>
<div><br></div></div></div>