Re: [Haskell-cafe] Rational and % operator remix
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)) --- On Sun, 3/29/09, Brandon S. Allbery KF8NH <allbery@ece.cmu.edu> wrote: From: Brandon S. Allbery KF8NH <allbery@ece.cmu.edu> Subject: Re: [Haskell-cafe] Rational and % operator remix To: "michael rice" <nowgate@yahoo.com> Cc: "Brandon S. Allbery KF8NH" <allbery@ece.cmu.edu>, "Lennart Augustsson" <lennart@augustsson.net>, haskell-cafe@haskell.org Date: Sunday, March 29, 2009, 4:27 PM On 2009 Mar 29, at 16:19, michael rice wrote:but I'm still at least one error short of a clean run 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 Where did I go wrong or what did I leave out? cf2 returns [Int], but you're using it as the tail of a list starting with ai, which is a Rational. Need to decide which you actually want. At a guess:
cf2 a = let ai = floor a> rai = toRational ai> in> if a == rai> then [ai]> else rai : cf2 (1 / (a - ai)) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.comsystem administrator [openafs,heimdal,too many hats] allbery@ece.cmu.eduelectrical and computer engineering, carnegie mellon university KF8NH
michael rice <nowgate@yahoo.com> writes:
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
participants (2)
-
Ketil Malde -
michael rice