了解功能构成

时间:2014-07-22 14:20:18

标签: haskell

虽然两者应该是等价的:

Prelude> :t \x -> fromIntegral (round x)
\x -> fromIntegral (round x) :: (Num b, RealFrac a) => a -> b
Prelude> :t fromIntegral . round
fromIntegral . round :: (Num c, RealFrac a) => a -> c

(它只是推断出的另一个占位符,但签名是等效的。)

Prelude> fromIntegral (round (pi * 100))
314

工作正常,但以下情况并非如此:

Prelude> fromIntegral . round (pi * 100)

<interactive>:34:1:
    No instance for (Integral b0) arising from a use of `fromIntegral'
    The type variable `b0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Integral Int -- Defined in `GHC.Real'
      instance Integral Integer -- Defined in `GHC.Real'
      instance Integral GHC.Types.Word -- Defined in `GHC.Real'
    In the first argument of `(.)', namely `fromIntegral'
    In the expression: fromIntegral . round (pi * 100)
    In an equation for `it': it = fromIntegral . round (pi * 100)

<interactive>:34:16:
    No instance for (Integral (a0 -> b0)) arising from a use of `round'
    Possible fix: add an instance declaration for (Integral (a0 -> b0))
    In the second argument of `(.)', namely `round (pi * 100)'
    In the expression: fromIntegral . round (pi * 100)
    In an equation for `it': it = fromIntegral . round (pi * 100)

再次,确实:

Prelude> fromIntegral . round $ pi * 100
314

- 我不明白,为什么,给定相同的签名,以及$与括号的等同性到行尾的括号版本不起作用?

我认为最后三个电话应该完全相同?

0 个答案:

没有答案