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

[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: David Allsopp Subject: RE: [Caml-list] Value shadowing (tangent)
```> Going off on a tangent here...

Indeed :o)

> > let lst = [5; 4; 3; 2; 1; 0; -1; -2; -3; -4; -5]
> > in
> >   let filter = List.filter (fun x -> x > 0)
> >   in
> >     let double = List.map (fun x -> -2 * x)
> >     in
> >       let sort = List.sort compare
> >       in
> >         (sort \$\$ double \$\$ filter) lst
>
> I've seen little of other people's OCaml code, so out of curiosity,
> do you or others actually write code formatted like the above, as
> opposed to the more compact and (I think) readable
>
>      let lst = [5; 4; 3; 2; 1; 0; -1; -2; -3; -4; -5] in
>      let filter = List.filter (fun x -> x > 0) in
>      let double = List.map (fun x -> -2 * x) in
>      let sort = List.sort compare in
>      (sort \$\$ double \$\$ filter) lst

I similarly learned OCaml without reference to anyone else's code - I find
the indentation invaluable when reading the code for spotting the scope. I
would normally have written the above as

let lst = [5; 4; 3; 2; 1; 0; -1; -2; -3; -4; -5]
and filter = List.filter (fun x -> x > 0)
and double = List.map (fun x -> -2 * x)
and sort = List.sort compare
in
(sort \$\$ double \$\$ filter) lst

Which is actually less to type than yours but I was adapting the previous
let lst = .. let lst = .. which is why I ended up with a very indented
version in my previous post.

I've not seen it anywhere else but then I haven't looked!

<irrelevance>
I don't know how the OCaml compiler actually implements displays, but the
"quick" approach means that using let .. and .. in instead of nested lets
will compile faster as name resolution will be faster :o)
</irrelevance>

David

```