Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Naming conventions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Krishnaswami, Neel <neelk@c...>
Subject: Re: [Caml-list] Naming conventions
Paul Stodghill [mailto:stodghil@CS.Cornell.EDU] wrote:
>
> In Scheme, there is a conventtion that the names of
> destructive functions end with "!" and predicates end with
> "?". E.g., "append!" vs. "append", and "null?", "pair?", etc.
>
> Are there any similar conventions that people use in O'Caml?

Yes. The usual convention is that mutating functions return the
unit value. Eg:

  Hashtbl.add : 'a t -> key -> data:'a -> unit (* Mutable collection   *)
  Map.add     : key -> 'a -> 'a t -> 'a t      (* Immutable collection *)

Each type usually has two iterators, as well -- one named "fold" for
the usual applicative folds, and another named "iter" for folding 
a destructive procedure over a datastructure. Eg:

  fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
  iter : (key -> 'a -> unit) -> 'a t -> unit

Since the imperative and applicative versions have different type, I 
suppose this convention might cause problems if you want to be 
able to transparently replace the pure versions with the destructive
versions. In that case, I'd suggest inventing your own convention;
perhaps you could use unprimed and primed names with the same types,
to denote pure and destructive operations. Eg;

  Mytype.add  : key -> 'a -> 'a t -> 'a t (* Pure *)
  Mytype.add' : key -> 'a -> 'a t -> 'a t (* Imperative *)

--
Neel Krishnaswami
neelk@cswcasa.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners