[Haskell-beginners] numerical types, the $ operator

Daniel Fischer daniel.is.fischer at web.de
Sun Mar 29 13:25:28 EDT 2009


Am Sonntag 29 März 2009 19:00:37 schrieb TG:
> On Sat, 28 Mar 2009 17:34 -0400, "Brent Yorgey" 
<byorgey at seas.upenn.edu>
>
> wrote:
> > > Moreover, I've put the 'RealFrac' by looking at ":t floor".
> > >
> > > What kind of class constraint whould you put for doing eg:
> > > >frac x = x - fromInteger (floor (sqrt x) )
> > >
> > > since 'floor' takes fractional and 'sqrt' takes RealFrac? Some 
kind of
> > > super-class?
> >
> > Every instance of RealFrac must also be an instance of Fractional, 
so
> > just putting RealFrac would be fine.  (And I didn't have this
> > memorized, I just started up ghci and typed ':info Fractional' and
> > ':info RealFrac' to see how they are declared.)
>
> Hmm, I gave the wrong types for floor and sqrt above (oops) but 
your
> answer should still be valid as a way of looking at things
> So
>     floor :: (RealFrac a, Integral b) => a -> b
>     sqrt :: (Floating a) => a -> a
> and looking as you suggest,
>     (Real a, Fractional a) => RealFrac a
>     (Fractional a) => Floating a
> So in this case the answer is...
>   frac :: (Floating a, RealFrac a) => a -> a
>   frac x = x - fromInteger (floor (sqrt x))
> No, common! Please tell me I'm wrong, that there's a simpler way!

Slightly simpler type signature:

frac :: RealFloat a => a -> a

Prelude> :i RealFloat
class (RealFrac a, Floating a) => RealFloat a where
  floatRadix :: a -> Integer
  floatDigits :: a -> Int
  floatRange :: a -> (Int, Int)
  decodeFloat :: a -> (Integer, Int)
  encodeFloat :: Integer -> Int -> a
  exponent :: a -> Int
  significand :: a -> a
  scaleFloat :: Int -> a -> a
  isNaN :: a -> Bool
  isInfinite :: a -> Bool
  isDenormalized :: a -> Bool
  isNegativeZero :: a -> Bool
  isIEEE :: a -> Bool
  atan2 :: a -> a -> a
        -- Defined in GHC.Float
instance RealFloat Double -- Defined in GHC.Float
instance RealFloat Float -- Defined in GHC.Float


>
>
> Clear, qualified answers and a bit of showing off for having 
something
> to aspire too. Thank you guys!
> --
>   TG
>   cowscanfly at airpost.net



More information about the Beginners mailing list