1
votes

Looking at the Operator Precedence table, I'm confused by where lambda expressions fit in to all of this. Numbering the table 1 to 25 (lowest to highest precedence), I see the two key tokens used for composing lambda expressions: fun is non-associative 6, and -> is right-associative 8. That is, fun ... -> .... This might be my first mistake in understanding, since I'm not even sure if precedence plays a role in the body of a lambda expression (the right hand side of ->) I can only think of precedence playing a role in the entire expression, so I'll move on to there. According to this table, the sequence operator, ;, has lower precedence, 4, than the fun "operator". Which I believe says that

fun (x:int) -> x + 3 ; 2;;

should be equivalent to

((fun (x:int) -> (x + 3)) ; 2);;

yet it is actually equivalent to

(fun (x:int) -> (x + 3 ; 2));;

I appreciate any help clarifying my interpretation of the rules of precedence given by the referenced table vs. the actual rules of precedence for lambda expressions! Thanks!

1

1 Answers

4
votes

MU

(If you are reading an operator precedence table, you've already lost. The table probably has bugs, but who cares, don't read the table. And don't use the sequence operator (;). And if you do use the sequence operator next to a lambda, then put the lambda in parentheses. Or just put every lambda in parentheses. I touch the code for the F# parser all the time, and I have no clue what the relative precedence of ; versus fun versus -> is, I don't even know what it means for -> to have a precedence (it is not an operator in expressions (only in types, where it is indeed right-associative)... I cannot imagine any satisfying answer to this question or any followup. You should have no interest in any of this unless you're writing your own parser/compiler for F#. These are not the droids you're looking for.)