I think that I could use fromEnum for a -> Int but how do I do it for a -> Float. I need my variable to be a Float for my function to work but I can’t work out how to convert it? I’ve gone through the Prelude and can’t see anything that would work. Sorry if this is a really stupid question but I’m just starting out.
Share
It depends on what a is. If a is an instance of Integral, like
IntorInteger, you can useIf a is an instance of Real, like Rational or Double, you can use
There are also special functions for Integer and Rational that
fromIntegralandrealToFracare based on:If a is an instance of Enum (and so you can use
fromEnum, as you said), thenfromIntegral . fromEnumshould do it.fromEnumconverts a toInt, thenfromIntegralconverts fromInttoFloat, sofromIntegral . fromEnumconverts a toFloat.