query about precedence: "$", the lazy function application operator
Hi, As I understand it, function application has highest precedence (10 even!) whereas $, the operator which does the same thing, has lowest precedence (0 even!). But there's something that doesn't make sense to me. Suppose I have functions f :: Int -> Int f x -> x * x g :: Int -> Int g x -> x + 1 The lazy application operator "$" allows me to do: f $ g x instead of f (g x) But I don't understand why it works this way! Let me explain. f is a function, and application has highest precedence, so unless it sees a bracket, it should take the next thing it sees as an argument. Lo and behold the next thing it sees is "$", which is not a bracket! So it should try and apply f to argument $. But oh dear, $ is a function (operator actually); it is not an Int at all! So an error should be reported! So what am I not understanding properly? Thanks, Mark. -- Dr Mark H Phillips Research Analyst (Mathematician) AUSTRICS - Smarter Scheduling Solutions - www.austrics.com Level 2, 50 Pirie Street, Adelaide SA 5000, Australia Phone +61 8 8226 9850 Fax +61 8 8231 4821 Email mark@austrics.com.au
f $ g x
using the notations from http://haskell.org/onlinereport/lexemes.html, `f' and `g' are varids, while `$' is a conid. see also http://haskell.org/onlinereport/exps.html, `f $ g x' is parsed as exp -> exp qop exp -> exp qop ( fexp ) -> exp qop (fexp axep) (regardless of precedences) best regards, -- -- Johannes Waldmann ---- http://www.informatik.uni-leipzig.de/~joe/ -- -- joe@informatik.uni-leipzig.de -- phone/fax (+49) 341 9732 204/252 --
Hi Mark,
Suppose I have functions
f :: Int -> Int f x -> x * x
I suppose you mean: f x = x * x
g :: Int -> Int g x -> x + 1
The lazy application operator "$" allows me to do:
f $ g x
instead of
f (g x)
But I don't understand why it works this way! Let me explain.
f is a function, and application has highest precedence, so unless it sees a bracket, it should take the next thing it sees as an argument.
Yes, but "$" cannot be an argument. In the Haskell grammar for expressions ( http://www.haskell.org/onlinereport/exps.html ) an application (fexp) consists of one or aexp's and an aexp cannot be an operator (at least not, without parentheses around it). A simpler way to see this is to write application as an explicit operator. Let's call it @. Above expression then reads f $ g @ x And @ binds stronger than $, alas f $ (g @ x) Arjan
participants (3)
-
Arjan van IJzendoorn -
Johannes Waldmann -
Mark Phillips