Undocumented featurette: Postfix operators

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Undocumented featurette: Postfix operators

AntC
There's an extension to GHC: 'Postfix Operators' .

Turns out Hugs can do that too (in Hugs mode).

(!) :: Num a => a -> a     -- note defined as monadic

> (!) 0 = 1                          -- lhs of equation must use prefix form

> (!) n = n *  ((n - 1) !)       -- but rhs can use postfix


So (3 !) returns 6, etc. The parens are needed so that it's parsed as a section.

I also took a leaf out of Oleg's book and built a variadic postfix operator
That is, (3 ~- 7) is parsed as 'subtract 3 from 7'. (3 ~-) is parsed as postfix negate 3 aka 'subtract 3 from zero'. It kinda worked, but the terms need a lot of explicit signatures and/or tricky type casting.


AntC

_______________________________________________
Hugs-Users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/hugs-users