Basically, I need to define infix operator for function composition, flipped g . f
manner.
(#) :: (a -> b) -> (b -> c) -> a -> c
(#) = flip (.)
infixl 9 #
Here, temporarily, I just have chosen the symbol: #
, but I'm not certain this choice is not problematic in terms of collision to other definitions.
The weird thing is somehow I could not find a Pre-defined infix operator list in Haskell.
I want the composition operator as concise as possible, hopefully, a single character like .
, and if possible, I want to make it &
. Is it OK?
Is there any guidance or best pracice tutorial? Please advise.
Related Q&A:
What characters are permitted for Haskell operators?
CodePudding user response:
You don't see a list of Haskell built-in operators for the same reason you don't see a list of all built-in functions. They're everywhere. Some are in Prelude
, some are in Control.Monad
, etc., etc. Operators aren't special in Haskell; they're ordinary functions with neat syntax. And Haskellers are generally pretty operator-happy in general. Spend any time inside your favorite lens library and you'll find plenty of amusing-looking operators.
In terms of (#)
, it might be best to avoid. I don't know of any built-in operators called that, but #
can be a bit special when it comes to parsing. Specifically, a compiler extension enables #
at the end of ordinary identifiers, and GHC defines a lot of built-in (primitive) types following this practice. For instance, Int
is the usual (boxed) integer type, whereas Int#
is a primitive integer. Most people don't need to interface with this directly, but it is a use that #
has in Haskell that would be confused slightly by the addition of your operator.
Your (#)
operator is called (>>>)
in Haskell, and it works on all Category
instances, including functions. Its companion is (<<<)
, the generalization of (.)
to all Category
instances. If three characters is too long for you, I've seen it called (|>)
in some other languages, but Haskell already uses that operator for something else. If you're not using Data.Sequence
, you could use that operator. But personally, I'd just go with (>>>)
. Any Haskeller will recognize it pretty quickly.