tags:

views:

192

answers:

2

These lines execute correctly:

Prelude> 1 / (1 + 1)
0.5
Prelude> (/) 1 $ (+) 1 1
0.5
Prelude> (/) 1 $ 1 + 1
0.5

This one does not:

Prelude> 1 / $ (+) 1 1

<interactive>:1:4: parse error on input `$'

Why?

+1  A: 

I believe that it is because $ is an operator that requires a function preceding it. The expression 1 / in your last example does not evaluate to a function. In that case, the parser is expecting to find a (numeric) expression as the second argument to the / operator.

Dave Costa
What you describe is a semantical explanation, and ghci returns parser error — that's a syntactic error. (Note: I am not arguing that that code should work ;)
Piotr Kalinowski
+14  A: 

/ is an infix operator. It requires valid expression on both its sides. 1 is a literal and thus a valid expression. However, on right-hand side you have immediately another infix operator, which requires to be preceded by another valid expression (and 1 / is not a valid expression, as it lacks right-hand side argument to the / operator). This is why parser reports error (invalid grammar — see haskell report for ugly details ;)

Piotr Kalinowski