[Haskell] Question about Infix/Postfix Operators in Haskell
Paul Hudak
paul.hudak at yale.edu
Mon Oct 18 08:40:07 EDT 2004
Pedro Vasconcelos wrote:
> On Mon, 18 Oct 2004 09:51:52 +0200
> "Georg Martius" <mai99dgf at studserv.uni-leipzig.de> wrote:
>>On Mon, 18 Oct 2004 09:43:26 +0200, Peter Theissen <peter at tsunamis.de> wrote:
>>>Hi,
>>>is there any possibility of defining Infix-/Postfixoperators
>>>in Haskell?
>>>Example:
>>>Plus :: Int, Int -> Int
>>>Plus x y = x + y
>>>an now I´m want to use Plus in another function as an infix:
>>>Times2:: x = x Plus x
>
> Another possiblity: define a new operator (let's call it $+) and you do
> without the backquotes:
>
> infixl 5 $+
> ($+) :: Int->Int->Int
> x $+ y = x+y
> times2 x = x $+ x
>
> This way you can specify the associativy and binding precedence and
> reduce the need for parenthesis.
Note that you can define associativity and precedence of ANY function
used as an infix operator, and you can also use it in an infix manner
when it is defined. For example:
> infixl 5 `plus`
> plus :: Int->Int->Int
> x `plus` y = x+y
> times2 x = x `plus` x
-Paul
More information about the Haskell
mailing list