[Haskell-cafe] noob question
Don Stewart
dons at galois.com
Mon Feb 25 19:57:16 EST 2008
thefunkslists:
> I'm just starting to learn Haskell, and I'm having some confusion (I
> think) with how the type inference is working. Can someone explain why,
> in ghc 6.8.2 this works:
> *Main> (1/3)^3
> 3.7037037037037035e-2
> But this doesn't
> *Main> (\k -> (1/k) ^ k) 3
> <interactive>:1:8:
> Ambiguous type variable `t' in the constraints:
> `Fractional t' arising from a use of `/' at <interactive>:1:8-10
> `Integral t' arising from a use of `^' at <interactive>:1:7-15
> Probable fix: add a type signature that fixes these type variable(s)
Oh, this is just that the / constrains the type to be
of class Fractional, while ^ only works on Integral values.
Try:
Prelude> (\k -> (1/k) ** k) 3
3.703703703703703e-2
Because:
Prelude> :t (^)
(^) :: (Integral b, Num a) => a -> b -> a
Prelude> :t (**)
(**) :: (Floating a) => a -> a -> a
Cheers,
Don
More information about the Haskell-Cafe
mailing list