In this Haskell program, @@
is an infix operator that I want to define only locally within the body of function f
. (Naturally enough, my actual program is more complicated than this, and there is a good reason to use infix notation.)
infixl 5 @@
(@@) = undefined
f x = x @@ 5 where x @@ y = (x+1) * (y+1)
main = print (f 7)
However, unless I also make the global definition, written here as (@@) = undefined
, GHC complains that 'The fixity signature for @@
lacks an accompanying binding.' Is there any way of getting round this without a global definition of the operator symbol?
Functions in Haskell default to prefix syntax, meaning that the function being applied is at the beginning of the expression rather than the middle. Operators are functions which can be used in infix style. All operators are functions.
What are infix functions? In R, most of the functions are “prefix” - meaning that the function name comes before the arguments, which are put between parentheses : fun(a,b) . With infix functions, the name comes between the arguments a fun b . In fact, you use this type on a daily basis with : , + , and so on.
Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).
Most of the operators that we use in R are binary operators (having two operands). Hence, they are infix operators, used between the operands. Actually, these operators do a function call in the background.
Just putting the fixity declaration in the where
clause seems to work fine:
f x = x @@ 5 where
infixl 5 @@
x @@ y = (x+1) * (y+1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With