Question about Infix/Postfix Operators in Haskell
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 Is this possible anyway? If this feature doesnt exist, could you please give some reasons for this? with kind regards, Peter Theissen
Hi, On Mon, 18 Oct 2004 09:43:26 +0200, Peter Theissen <peter@tsunamis.de> wrote:
Hi, is there any possibility of defining Infix-/Postfixoperators in Haskell?
Example: Plus :: Int, Int -> Int
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
Times2:: x = x `Plus` x Best regards! Georg -- ---- Georg Martius, Tel: (+49 34297) 89434 ---- ------- http://www.flexman.homeip.net ---------
On Mon, 18 Oct 2004 09:51:52 +0200 "Georg Martius" <mai99dgf@studserv.uni-leipzig.de> wrote:
Hi,
On Mon, 18 Oct 2004 09:43:26 +0200, Peter Theissen <peter@tsunamis.de> wrote:
Hi, is there any possibility of defining Infix-/Postfixoperators in Haskell?
Example: Plus :: Int, Int -> Int
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
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. Best regards, Pedro -- Pedro Vasconcelos, School of Computer Science, University of St Andrews ----------------------------------------------------------------------- "The difference between Theory and Practice is greater in Practice than in Theory."
Lest it all be explanation by example, the story basically goes like this: Binary functions can be used in infix style by surrounding the function name by backticks (plus x y <=> x `plus` y) Binary operators can be used in prefix style by enclosing it in parentheses ( 4 + 3 <=> (+) 4 3 ) And just like you can declare new "normal" functions, you can declare new infix operators by using non-alpha names. -kzm -- If I haven't seen further, it is by standing in the footprints of giants
Pedro Vasconcelos wrote:
On Mon, 18 Oct 2004 09:51:52 +0200 "Georg Martius" <mai99dgf@studserv.uni-leipzig.de> wrote:
On Mon, 18 Oct 2004 09:43:26 +0200, Peter Theissen <peter@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
participants (5)
-
Georg Martius -
Ketil Malde -
Paul Hudak -
Pedro Vasconcelos -
Peter Theissen