[Haskell-cafe] Rational and % operator remix

Ketil Malde ketil at malde.org
Mon Mar 30 02:48:18 EDT 2009


michael rice <nowgate at 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


More information about the Haskell-Cafe mailing list