0
(head.group) "1234"

It work.

head.group "1234"

I get errors:

<interactive>:8:6:
Couldn't match expected type `a0 -> [c0]'
            with actual type `[[Char]]'
In the return type of a call of `group'
Probable cause: `group' is applied to too many arguments
In the second argument of `(.)', namely `group "1234"'
In the expression: head . group "1234"

I think (head.group) is same as head.group, why (head.group) work and head.group not.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
mc.robin
  • 179
  • 1
  • 10
  • I think part of the confusion may be resulting from the unorthodox formatting of your code. What you've written as `head.group "1234"` would normally be written equivalently as `head . group "1234"`. Also, just in case this isn't already clear, it's important to understand that `.` is not a special construct in the language; it is [an ordinary function](https://www.stackage.org/haddock/lts-8.20/base-4.9.1.0/Prelude.html#v:.). – Chris Martin Jun 27 '17 at 07:10
  • @ChrisMartin But as soon as `.` is in infix position, it is evaluated as an operator, right? And normal function application has precedence over operators. So `(.) head group "1234"` would work. –  Jun 27 '17 at 08:08
  • @ftor right, because `(.) head group "1234"` is the same as `(((.) head) group) "1234" = ((.) head group) "1234" = (head . group) "1234"`. – Zeta Jun 28 '17 at 05:56

1 Answers1

4

Because

(head . group) "1234" = f "12345"
  where
    f = head . group

whereas

head . group "1234" = head . (group "1234") 
                    = head . f
  where
    f = group "1234"

but group "1234" isn't a function. Remember, function application binds stronger than operators.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Thank you. A anthor question, why let f = head.group, f [1,2, 3] get a error – mc.robin Jun 27 '17 at 06:36
  • this is the error info: :4:4: No instance for (Num ()) arising from the literal `1' Possible fix: add an instance declaration for (Num ()) In the expression: 1 In the first argument of `f', namely `[1, 2, 3]' In the expression: f [1, 2, 3] – mc.robin Jun 27 '17 at 06:44
  • That's the [monomorphism restriction](https://stackoverflow.com/questions/32496864/what-is-the-monomorphism-restriction). It should not happen in GHCi. Please not that interpreted code slightly differs from compiled one. – Zeta Jun 27 '17 at 06:50
  • Maybe mentioning the term [composition](https://www.stackage.org/haddock/lts-8.20/base-4.9.1.0/Prelude.html#v:.) in your answer would be informative. – Wolf Jun 27 '17 at 07:29