Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] Future of labels
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-04-09 (15:19)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] Future of labels, and ideas for library labelling
Jacques Garrigue wrote:

> >  1.  List.fold_right (List.fold_right IntSet.add) lists IntSet.empty
> >
> >      How can I use labels here without eta expansion?
> So, first let me see what your function is intending to do.
> Ah, yes, your lists argument is a list of lists, and you want to put
> everything into a set.
> Now let's write it with labels
>    List.fold_left lists ~init:IntSet.empty
>      ~f:(fun set l ->
>            List.fold_left l ~init:set ~f:(fun set x -> IntSet.add x set))
> Note that I've switched to List.fold_left, which is tail recursive,
> but you cannot do it with your approach.
> I have also eta-expanded more than necessary, to make the code more
> readable.
> We can write something shorter if we use labels as they were in ocaml
> 2.99, before their trimming:
>    List.fold_left lists ~acc:IntSet.empty
>      ~f:(List.fold_left ~f:(fun ~acc item -> IntSet.add acc ~item))
> Now, which is the more readable program, this one or yours.

	I'm a label mode supporter using classic mode exclusively.
I have no problem reading the original expression. Both the
label mode versions are harder for me to read due to a lot
of extra clutter. However, were the example more complex,
I think the label mode would win. More precisely: I didn't
actually _read_ the original example: I mentally pattern-matched
it in a single viewing. I had to scan the label mode versions
multiple times. 
> Can you tell at first sight if this code is correct or not?

	No, but in this case, I think the label mode version is
easier to check. I just 'guessed' what the classic mode
version of the expression did, i didn't deduce it: its a common
idiom I'm familiar with.
> My experience, and that of Wolfram Kahl, is that the eta-expansions
> you have to do when labels get in the way are more useful than
> harmful. Hard to grasp at the beginning, but clearly there is good
> verbosity and bad verbosity. Making the labels apparent is generally
> good verbosity.

	My take is this: if it is possible to recognize something
'as a whole' in a compact form, the compact form is best.
Otherwise, it is better to have enough redundancy that each part
is easily connected with the others while scanning the parts.
That is, classic mode is superior for simple things but doesn't scale.

	Here they are again, further reduced:

   fold_right (fold_right add) lists empty

   fold_left lists ~init:empty
      ~f:(fun set l ->
            fold_left l ~init:set ~f:(fun set x -> add x set))

The latter introduces extra names 'set' and 'l' and 'x',
where 'set' has two distinct definitions, in addition to
having the extra labels 'init' and 'f', which is 5 extra words.
It also uses 4 twiddles and 4 colons (~:), and an extra pair
of brackets, and takes three lines compared to one.
I find the layout disturbing. I'd write:

	    (fun set l ->
	      ~f(fun set x -> add x set)

which is 10 lines! Ugh.

On the other hand, for a callback from a GUI function, the more
longwinded style would probably help.

John (Max) Skaller,
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper
download Interscript
To unsubscribe, mail  Archives: