Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with round with Data.Fixed

Tags:

haskell

I have defined the following type in myfile.hs:

{-# LANGUAGE DeriveDataTypeable #-}
import Data.Typeable
import Data.Fixed

data E18 = E18 deriving (Typeable)
instance HasResolution E18 where resolution _ = 10^18
type MyDouble = Fixed E18

--myRound :: MyDouble > MyDouble 
--myRound x  = round x

and whereas the round function works well in ghci :

Prelude Data.Fixed> :load myfile.hs
Prelude Data.Fixed> round (3.1::MyDouble)
3
Prelude Data.Fixed>

if I append the following code to the file :

myRound :: MyDouble -> MyDouble 
myRound x  = round x

I get the following compilation error :

Prelude Data.Fixed> :load myfile.hs
No instance for (Integral MyDouble)
  arising from a use of round

Could someone please tell me how to call round inside a function ?

PS: As you may guess, I'm beginner in Haskell

like image 819
kevin Avatar asked Dec 05 '25 04:12

kevin


1 Answers

round takes one RealFrac and produces a Integral:

round :: (Integral b, RealFrac a) => a -> b

Your myRound should have about the same type as round: take one MyDouble and produce an Int. So instead of MyDouble -> Int -> MyDouble, it should be MyDouble -> Int, since (I guess) what you need in the end is an Int.

Also note instead of writing

myRound x = round x

you can just say

myRound = round

If you want myRound produce you a MyDouble, then

myRound :: MyDouble -> MyDouble 
myRound  = fromIntegral . round

should work.

like image 194
zw324 Avatar answered Dec 07 '25 18:12

zw324



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!