0

While the two should be equivalent:

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

(it's just a different placeholder that is inferred, but the signatures are equivalent).

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

works fine, but the following does not:

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)

this again, does:

Prelude> fromIntegral . round $ pi * 100
314

-- I don't understand, why, given the same signature, and also the equivalence of $ to parentheses to the end of line the parenthesis version does not work?

I thought the last three calls should be completely equivalent?

A Sz
  • 984
  • 7
  • 19
  • `fromIntegral . round $ pi * 100` =~= `(fromIntegral . round) (pi * 100)` -- the `$` is to low-precedence that it is done after all other operators, effectively parenthesizing the whole of the line *before* and after it. – Boyd Stephen Smith Jr. Jul 22 '14 at 17:44
  • 2
    Function application has highest precedence, `fromIntegral . round (pi * 100)` is `\x -> fromIntegral (round (pi * 100) x)` – user2407038 Jul 23 '14 at 01:13

0 Answers0