Right associative operator in F#

f#

Solution

There is no way to define custom operator with an explicitly specified associativity in F# - the associativity is determined based on the symbols forming the operator (and you can find it in the MSDN documentation for operators).

In this case, F# does not have any built-in operator that would let you avoid the parentheses and the idiomatic way is to write the code as you write it originally, with parentheses:

myList |> List.iter (fun x -> x)

This is difference in style if you are coming from Haskell, but I do not see any real disadvantage of writing the parentheses - it is just a matter of style that you'll get used to after writing F# for some time. If you want to avoid parentheses (e.g. to write a nice DSL), then you can always named function and write something like:

myList |> List.iter id

(I understand that your example is really just an example, so `id` would not work for your real use case, but you can always define your own functions if that makes the code more readable).

Problem

Sometimes I have to write: ``` myList |> List.iter (fun x -> x) ``` I would really like to avoid the parentheses. In Haskell there is an operator for this ($) It would look like this ``` myList |> List.iter $ fun x -> x ``` I created a custom operator ``` let inline (^!) f a = f a ``` and now I can write it like this ``` myList |> List.iter ^! fun x -> x ``` Is there something like this in F#?

Original source

Related problems