user-defined operators

Carsten Schultz <>
Thu Mar 25 10:05:42 CET 2004


On Wed, Mar 24, 2004 at 09:38:28PM -0500, Shawn Pearce wrote:
> Ulf Wiger <> wrote:
> > So is your suggestion that one would write e.g.
> > 
> >   -import(sys, ['!!'/2,]).
> > 
> >   fetch(Host, File) ->
> >      Host `!!` {get, File}.
> 
> Bleh!
> 
> 	spearce `wontuse` backticks_for_operators.

Just for those who don't know, and because Haskell has been mentioned
as an inspiration (and sorry, I do not know the correct terminology):
In Haskell you can define your own operators, and operators and
functions (ie ordinary variables) are distinguished by the characters
they are made of.  Example operator: +++.  Example function: plus.
You can define your own of both.  The clever bit is that by enclosing
an operator name in parentheses, you make it an ordinary variable, and
by enclosing a variable in backticks you make it an infix operator.
So you can define

(+++) :: [Int] -> Int -> [Int]

(+++) (x:xs) y = (x+y) : xs

to add an Int to the first element of a list of Ints and then use this
as

[1,2,3] +++ 5

(yielding [6,2,3]) or

(+++) [1,2,3] 5

as you like.

Or you could have defined

plus :: [Int] -> Int -> [Int]

plus (x:xs) y = (x+y) : xs

and used it as

[1,2,3] `plus` 5

or

plus  [1,2,3] 5


Greetings,

Carsten

-- 
Carsten Schultz (2:38, 33:47), FB Mathematik, FU Berlin
http://carsten.codimi.de/
PGP/GPG key on the pgp.net key servers, 
fingerprint on my home page.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20040325/944f9975/attachment.bin>


More information about the erlang-questions mailing list