[Haskell-beginners] numerical types, the $ operator
TG
cowscanfly at airpost.net
Sun Mar 29 13:00:37 EDT 2009
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!
Clear, qualified answers and a bit of showing off for having something
to aspire too. Thank you guys!
--
TG
cowscanfly at airpost.net
--
http://www.fastmail.fm - Does exactly what it says on the tin
More information about the Beginners
mailing list