Like Haskell, Fβ― also allows you to define new infix operators, not just overload existing ones. Unlike Haskell however, you can't select precedence (aka fixity) at will, nor select any non-letter Unicode symbol. Instead, you must choose from !
, $
, %
, &
, *
, +
, -
, .
, /
, <
, =
, >
, ?
, @
, ^
, |
. The fixity is then determined by analogy with the standard operators, in a way I don't entirely understand but what does seem to hold is that for any single-symbol infix, you can make a custom one with the same fixity by adding a .
in front.
So, to get a lowest-fixity operator you'd have to call it .|
. However, |
is left-associative, so you couldn't write printfn "%d" .| abs .| 3 - 5
. However I'd note that in Haskell, your example would also rather be written print . abs $ 3 - 5
, and that can indeed be expressed in Fβ―:
let (.|) f x = f x
printfn "%d" << abs .| 3 - 5
To transliterate print $ abs $ 3 - 5
, you'd need a right-associative operator. The lowest-precedence custom right-associative operator I can manage to define is .^
, which indeed gets the example working:
let (.^) f x = f x
printfn "%d" .^ abs .^ 3 - 5
However, this operator doesn't have very low precedence, in particular it actually has higher precedence than the composition operators!
Really, you shouldn't be doing any of this and instead just use printfn "%d" << abs <| 3 - 5
as you originally suggested. It's standard, and it also corresponds to the preferred style in Haskell.
<<
or<|
(I work primarily in F# and I don't use them at all). You can always express things going "forwards" with>>
and|>
. It's a very intentional stylistic difference. This example would become3 - 5 |> abs |> printfn "%d"
. Something to bear in mind if you plan to write F# for others to read. In this particular example I would simply add parensprintfn "%d" (abs (3 - 5))
. Perfectly non-clever! π β Collectivity$
is used there. The Haskel binary operator symbol$
has no relation to it. β Drifterprintfn
. Which is actually purely on purpose for printing the result of the implementation of the binary operator that is the target of this QA. β Drifter