Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Having '<<', why to use '|>' ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Aaron Bohannon <bohannon@c...>
Subject: Re: [Caml-list] Having '<<', why to use '|>' ?
It's too bad that $ is reserved for Camlp4.  It would be a natural
choice for infix application since that's what Haskell uses.

I have used the following notations for function composition in the past:

let ( |> ) f g x = g (f x)
let ( <| ) f g x = f (g x)

I am glad that those are natural to other people, too.  I also like to
be able to compose predicate functions.  Does anyone have better ideas
than what is below?

let ( &&& ) p q x = p x && q x
let ( ||| ) p q x = p x || q x
let ( !!! ) p x = not (p x)

One can also define "%" to do Python-esque string construction,
although using it with more than one argument requires a nice operator
for infix application.

let ( % ) f x = Format.sprintf f x

"(%c, %n, %s, %b)" % 'a' @@ 12 @@ "hello" @@ true

"@@" isn't looking so nice to me here.

And an operator that is very useful (almost essential) in certain
situations is a monadic "bind" in the list monad:

let ( >>@ ) xs f = List.flatten (List.map f xs)

I chose "@" as a reference to the list concatenation operator.

Are there any others?  Are there better ideas or warnings about the
ones I have here?

-Aaron

> ---------- Forwarded message ----------
> From: Fabrice Marchant <fabrice.marchant@orange.fr>
> To: caml-list@yquem.inria.fr
> Date: Tue, 18 Sep 2007 16:12:46 +0200
> Subject: Re: [Caml-list] Having '<<', why to use '|>' ?
>   Thanks Julien !
>
> > Have a look at this:
> >       http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#infix-symbol
> > With the keywords below.
> "Note that the following identifiers are keywords of the Camlp4 extensions and should be avoided for compatibility reasons.
>
>     parser    <<    <:    >>    $     $$    $:
> "
> So no doubt, I'll edit my old programs and replace "<<".
>
> > > ... a composition operator ... ( <<< ) ?
> > > What else ?
>
> > I would personally double the '@':
> >       let (@@) f g x = f @ g x
>
>  ( or f (g x) : it is practically the same thing. )
>
> A 3 chars operator (<<<) doesn't look smart. Simpler is better.
> However, about (@@), I preferred to see the direction of the asymmetric composition operator.
> ( <| ) instead of ( << ) ? Is this a possible idea ?
>
>   But maybe your idea is good. Maths use a kind of small 'o' : (f o g) (x) = f (g (x)).
> It's symmetric like (@@), and that doesn't raise any problem.
>
> Cheers,
>
> Fabrice