[Haskell-cafe] type keeping rounding, typeable (and a difficulty)

Greg Buchholz haskell at sleepingsquirrel.org
Wed Nov 15 16:31:30 EST 2006


isto wrote:
] I've been trying to compile the following function
] (rounding to a desired degree):
] 
] roundDec :: (Num a, Typeable a) => Int -> a -> a
] roundDec d a = 
] 	let t = show (typeOf a)
] 	in case t of
] 		"Double"  -> roundDDec d a
] 		"Complex Double" -> roundCDec d a
] 		otherwise -> a  -- or something
] 
] The two other functions are 
] 
] roundCDec :: (RealFloat a) => Int -> Complex a -> Complex a
] roundCDec d (c :+ b) = (roundDDec d c :+ roundDDec d b)
] and
] roundDDec :: (RealFloat a) => Int -> a -> a
] roundDDec d a = a  -- or somegthing

    Maybe you want type classes instead?

> import Complex
> 
> class Round a where
>     roundD :: Int -> a -> a
> 
> instance Round Double where
>     roundD d a = a
> 
> instance (Round a, RealFloat a) => Round (Complex a) where
>     roundD d (c :+ b) = (roundD d c :+ roundD d b)



More information about the Haskell-Cafe mailing list