From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA12870; Sat, 31 Jul 2004 16:03:48 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA12339 for ; Sat, 31 Jul 2004 16:03:47 +0200 (MET DST) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i6VE3jEV016593 for ; Sat, 31 Jul 2004 16:03:46 +0200 Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.11.6/8.10.1) with ESMTP id i6VE3go23783; Sat, 31 Jul 2004 09:03:42 -0500 (CDT) Date: Sat, 31 Jul 2004 09:11:52 -0500 (CDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: "Christopher A. Gorski" cc: Ocaml Mailing List Subject: Re: [Caml-list] const equivalent for mutable types? In-Reply-To: <410B5EBD.6060800@cgorski.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at nez-perce with ID 410BA6C1.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 const:01 passing:01 reproducing:01 const:01 passing:01 foo:01 char:01 val:01 foo:01 modifies:01 val:01 implemented:01 unmodified:01 bool:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, 31 Jul 2004, Christopher A. Gorski wrote: > In my code I find that I'm passing a lot of mutable values to functions. > Some functions merely read the values. Others modify the values. Is > there a method in OCaml for reproducing behavior similar in spirit to > the const declaration in C? Yeah. Don't pass in a reference, pass in what the reference points at. If you're passing a lot of mutable arguments around, I'd start rethinking your design. Mutable arguments should be few and far between. Two functional design patterns you might not be aware of, that should help eliminate a lot of mutable arguments: 1) Use tuples to return multiple values. One of the most common reasons in C/C++/Java programming to using mutable arguments is to return multiple values from a function. Say you have a function that takes an integer and a string, and returns a boolean and a float. In C/C++, you might have a header something like: bool foo(int arg1, char * arg2, double * res2_p); res2_p is the pointer to the location to store the second result, the float. In Ocaml, the function should have the type: val foo: int -> string -> bool * float The advantage of doing this is in Ocaml is that the caller can drop the return values into whatever variables they want, like: let succeeded, fval = foo(3, "bar") in ... It also makes what is an input to the function and what is an output of the function clear. 2) Return the updated data structure. Another common reason to use mutable arguments is to pass in a data structure that the routine then modifies. Instead of having the data structure be modified, have the routine return the replacement data structure. Say you have a StringMap module, defined like: module StringMap = Map.Make(String);; You want to write a routine that adds the key "foo" and the value 3 to an int StringMap.t- thus modifying the data structure. The function should have the type: val add_foo: int StringMap.t -> int StringMap.t And might be implemented like: let add_foo smap = StringMap.add "foo" 3 smap;; The big advantage here is that the caller gets to decide which version of versions of the data structure they continue to use- the old, unmodified version, or the new, modified version, or both. -- "Usenet is like a herd of performing elephants with diarrhea -- massive, difficult to redirect, awe-inspiring, entertaining, and a source of mind-boggling amounts of excrement when you least expect it." - Gene Spafford Brian ------------------- 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