Re: [Haskell-cafe] Rational and % operator remix

My query was in response to an earlier post claiming that ai was a Rational, which post he almost simultaneously said to ignore.
I've used GHC and like it better than Hugs, but the Fedora Linux package is humongous and I'm only on dialup right now.
I'm still not sure what some of the error messages I was getting were about. As I wrote the function I tried to be aware of what "mixed mode" operations were kosher ala
Main> 1 / (6%7)
7 % 6
Main> 1 - (6%7)
1 % 7
Main> (6%7) - 1
(-1) % 7
Main>
but still kept getting kicked out.
Better error messages certainly couldn't hurt.
Thanks.
Michael
--- On Mon, 3/30/09, Ketil Malde
cf2 :: Rational -> [Int] cf2 a = let ai = floor a <-- Doesn't this make ai an Int? -Michael in if a == (toRational ai) then [ai] else ai : cf2 (1 / (a - ai))
One thing that you could try, is ghci in addition to (or instead of) Hugs. That will give you another take on the error messages, where Hugs says:
Main> :load cf.hs ERROR "cf.hs":7 - Type error in application *** Expression : ai : cf2 (1 / (a - ai)) *** Term : ai *** Type : Ratio Integer *** Does not match : Int
ghci will tell you: Couldn't match expected type `Rational' against inferred type `Int' In the second argument of `(-)', namely `ai' In the second argument of `(/)', namely `(a - ai)' In the first argument of `cf2', namely `(1 / (a - ai))' So while this is (almost) right:
cf2 a = let ai = floor a <-- Doesn't this make ai an Int? -Michael
The problem is the subtraction of ai from a, which forces them to be the same type. I tend to find ghc's messages more informative than Hugs's (but perhaps it is just that I am more familiar with ghc?). -k -- If I haven't seen further, it is by standing in the footprints of giants

2009/3/30 michael rice
I'm still not sure what some of the error messages I was getting were about. As I wrote the function I tried to be aware of what "mixed mode" operations were kosher ala
This is a mistake, but understandable given your lispy background; there aren't really "mixed mode" operations in Haskell. One thing that might be confusing you: Numeric literals are really calls to "fromInteger"; that is, "5" is really "fromInteger (5 :: Integer)" Generally you will find that :t in the interpreter is your friend: ghci> :t 1 1 :: (Num t) => t This says that the expression "1" can be of any type that is an instance of Num. ghci> :t (\a b -> a - b) (\a b -> a - b) :: (Num a) => a -> a -> a "-" takes two arguments that are of the same type, and returns something of that type. ghci> :m Data.Ratio ghci> :t (%) (%) :: (Integral a) => a -> a -> Ratio a "%" lifts objects from an integral type into a type that represents the ratio of two integers. ghci> :t floor floor :: (RealFrac a, Integral b) :: a -> b So "floor" is a "cast" operation that converts between any fractional type to any integral type. This is why you need to either use "fromInteger" or "%" on the result of "floor" to get it back as a Rational. -- ryan
participants (2)
-
michael rice
-
Ryan Ingram