Juan Cruz
Juan Cruz

Reputation: 11

Convert Ord to Fractional

How do I convert an Ord number given by max function into a fractional. Or how do I convert a number with 2 constraints (x::(Ord a, Fractional a) => a) into an int?

don't worry about RE

seemore :: (Fractional a) => RE b -> a -> a
seemore (a:+:b) x = seemore (a) (x) + seemore b x
seemore (a:|:b) x = max (seemore a y) (seemore b z)
   where y = x
         z = x
seemore (Symbol a) x = x
seemore Empty x = 0
seemore (Repeat(a)) x = 1/0
seemore (Plus(a)) x = 1/0

in the "seemore :: (Fractional a)" part I need to add Ord a into that constraint, or else it fails. But then I'm stuck with a number that is a fractional and ord though there are no functions that convert that number into an int. Ideally I want a function that converts an ord to a fractional.

Upvotes: 1

Views: 244

Answers (1)

moonGoose
moonGoose

Reputation: 1510

Ideally I want a function that converts an ord to a fractional.

Many more things can be ordered than are fractional, eg. "abcd" < "abce", but what is "abcd" / "abce"?

I'm stuck with a number that is a fractional and ord though there are no functions that convert that number into an int.

If you all know is Fractional and Ord, how do you know a sensible -> Integer conversion exists? For instance, consider Data.Complex Double (basically just a complex number) - we can add, subtract, divide even (ie. it is Fractional) but what would make mathematical sense as an answer for toInteger (1 + i)? There isn't a good definition.

It's unclear what exactly you're trying to achieve, but, the closest I can think of is, adding a RealFrac a constraint. This would provide you with the functions you want (eg. truncate, round, ceiling, floor) for converting to Integral types.

Upvotes: 5

Related Questions