Infix expressions

Jon Fairbairn Jon.Fairbairn@cl.cam.ac.uk
Mon, 29 Jul 2002 21:25:40 +0100


Ken Shan <ken@digitas.harvard.edu> wrote:
> In Haskell, backquotes can be used to convert individual identifiers
> into infix operators, but not complex expressions.  For example,
> =

>     [1,2,3] `zip` [4,5,6]
> =

> is OK, but not
> =

>     [1,2,3] `zipWith (+)` [4,5,6]
> =

> Is there any reason other than potential confusion when one of the two
> backquotes is accidentally omitted?

I've often wondered about this myself, but it's difficult to
make a pleasant distinction between what's allowed in
between `` and an ordinary expression. They can't be the
same because you can't nest them. Using a matched pair of
quotation marks would work, but then you have the
possibility of writing really horrid expressions.

> In any case, perhaps some people on this mailing list would appreciate
> the following implementation of "infix expressions" that Dylan Thurston=

> and I came up with -- as algebraic and perverse as we could manage:
> =

>     infixr 0 -:, :-
>     data Infix f y =3D f :- y
>     x -:f:- y =3D x `f` y
> =

>     main =3D print $ [1,2,3] -: zipWith (+) :- [4,5,6]

Yes, I appreciate that! It reminds me of how I got the
syntax of Ponder -- which had no predefined operators, not
even "if" -- to work.

> The trick is that there is no trick.

Oh, I think it /is/ a trick :-)

  J=F3n

-- =

J=F3n Fairbairn                                 Jon.Fairbairn@cl.cam.ac.u=
k
31 Chalmers Road                                         jf@cl.cam.ac.uk
Cambridge CB1 3SZ            +44 1223 570179 (after 14:00 only, please!)