Hello, I am having a problem. I recently desided I wanted a bunch function to return float instead of Int. I changed their type and wrote a new function that returned a float. I figured it'd be okay if all the others still returned Int since it's trivial to convert Int to Float. Sadly Haskell won't let me do this. What should I do? I attempted to cast all of the values in the functions that returned Int to Float, but I found no way to do this. I found fromInteger but it didn't seem to work on the return value of the cardinality function for instance. I guess cardinality much return Int. This is one of my functions. smallestSet :: GameState -> Float smallestSet s = (-1 * cardinality (fLocations s)) This function is an error because it infer's an Int. Thanks, -mike
Try intToFloat :: Int -> Float intToFloat n = fromInteger (toInteger n) -W-M- @ @ | \_/ On Fri, 28 Feb 2003, Mike T. Machenry wrote:
Hello,
I am having a problem. I recently desided I wanted a bunch function to return float instead of Int. I changed their type and wrote a new function that returned a float. I figured it'd be okay if all the others still returned Int since it's trivial to convert Int to Float. Sadly Haskell won't let me do this. What should I do? I attempted to cast all of the values in the functions that returned Int to Float, but I found no way to do this. I found fromInteger but it didn't seem to work on the return value of the cardinality function for instance. I guess cardinality much return Int. This is one of my functions.
smallestSet :: GameState -> Float smallestSet s = (-1 * cardinality (fLocations s))
This function is an error because it infer's an Int.
Thanks, -mike _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Use the Prelude function realToFrac. -- Lennart Mike T. Machenry wrote:
Hello,
I am having a problem. I recently desided I wanted a bunch function to return float instead of Int. I changed their type and wrote a new function that returned a float. I figured it'd be okay if all the others still returned Int since it's trivial to convert Int to Float. Sadly Haskell won't let me do this. What should I do? I attempted to cast all of the values in the functions that returned Int to Float, but I found no way to do this. I found fromInteger but it didn't seem to work on the return value of the cardinality function for instance. I guess cardinality much return Int. This is one of my functions.
smallestSet :: GameState -> Float smallestSet s = (-1 * cardinality (fLocations s))
This function is an error because it infer's an Int.
Thanks, -mike _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
"Mike T. Machenry" <dskippy@ccs.neu.edu> writes:
I recently desided I wanted a bunch function to return float instead of Int. [...] I found fromInteger but it didn't seem to work on the return value of the cardinality function for instance.
Try fromIntegral, which works for Int and Integer, too. Feri.
"Mike T. Machenry" <dskippy@ccs.neu.edu> writes:
I recently desided I wanted a bunch function to return float instead of Int. [...] I found fromInteger but it didn't seem to work on the return value of the cardinality function for instance.
Try fromIntegral, which works for Int and Integer, too.
Casting an Integral value to a Fractional value to perform arithmetic operations, is a very common need and I don't like adding fromIntegral everywhere, so ended up writing a (very simple) module with generalized arithmetic operators (see attachment). The » next to the operations indicate a cast from an Integral to a Fractional value. J.A.
Thank does sound like a pain, but it's better than putting fromIntegral all over my code. Why can't Haskell unify a an expected float with an infered int? It seems that this would make life alot easier. -mike On Sun, Mar 02, 2003 at 11:28:00AM +0000, Jorge Adriano wrote:
"Mike T. Machenry" <dskippy@ccs.neu.edu> writes:
I recently desided I wanted a bunch function to return float instead of Int. [...] I found fromInteger but it didn't seem to work on the return value of the cardinality function for instance.
Try fromIntegral, which works for Int and Integer, too.
Casting an Integral value to a Fractional value to perform arithmetic operations, is a very common need and I don't like adding fromIntegral everywhere, so ended up writing a (very simple) module with generalized arithmetic operators (see attachment). The � next to the operations indicate a cast from an Integral to a Fractional value.
J.A.
module CrossTypeOps where
-- Addition (+�) :: (Fractional a, Integral b)=> a -> b -> a (+�) x n = x+fromIntegral n
(�+) :: (Integral a, Fractional b)=> a -> b -> b (�+) n x = fromIntegral n + x
(�+�) :: (Integral a, Fractional b)=> a -> a -> b (�+�) m n = fromIntegral m+fromIntegral n
-- Difference (-�) :: (Fractional a, Integral b)=> a -> b -> a (-�) x n = x-fromIntegral n
(�-) :: (Integral a, Fractional b)=> a -> b -> b (�-) n x = fromIntegral n - x
(�-�) :: (Integral a, Fractional b)=> a -> a -> b (�-�) m n = fromIntegral m-fromIntegral n
-- Multiplication (*�) :: (Fractional a, Integral b)=> a -> b -> a (*�) x n = x*fromIntegral n
(�*) :: (Integral a, Fractional b)=> a -> b -> b (�*) n x = fromIntegral n * x
(�*�) :: (Integral a, Fractional b)=> a -> a -> b (�*�) m n = fromIntegral m*fromIntegral n
-- Division (/�) :: (Fractional a, Integral b)=> a -> b -> a (/�) x n = x / fromIntegral n
(�/) :: (Integral a, Fractional b)=> a -> b -> b (�/) n x = fromIntegral n / x
(�/�) :: (Integral a, Fractional b)=> a -> a -> b (�/�) m n = fromIntegral m / fromIntegral n
-- Priorities infixl 6 +�, �+, �+�, -�, �-, �-� infixl 7 *�, �*, �*�, /�, �/, �/�
"Mike T. Machenry" <dskippy@ccs.neu.edu> writes:
I am having a problem. I recently desided I wanted a bunch function to return float instead of Int. I changed their type and wrote a new function that returned a float. I figured it'd be okay if all the others still returned Int since it's trivial to convert Int to Float.
Perhaps the other functions could be written with a more general type? (E.g. :: Num a => ... -> a) Try to remove the type declaration, and see what Hugs or GHCi :t has to say about it! -kzm -- If I haven't seen further, it is by standing in the footprints of giants
participants (6)
-
Ferenc Wagner -
Jorge Adriano -
ketil@ii.uib.no -
Lennart Augustsson -
Mike T. Machenry -
Wang Meng