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 KAA07839; Fri, 11 Jan 2002 10:10:54 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA07113 for caml-list@pauillac.inria.fr; Fri, 11 Jan 2002 10:10:53 +0100 (MET) 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 OAA05678 for ; Tue, 8 Jan 2002 14:21:30 +0100 (MET) Received: from libcom.com ([208.167.88.73]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g08DLT112148 for ; Tue, 8 Jan 2002 14:21:29 +0100 (MET) Received: (from jprevost@localhost) by libcom.com (8.11.6/8.11.6) id g08DQd789651; Tue, 8 Jan 2002 08:26:39 -0500 (EST) (envelope-from jprevost@libcom.com) Date: Tue, 8 Jan 2002 08:26:39 -0500 From: John Prevost To: caml-list@inria.fr Cc: romildo@uber.com.br Subject: Re: [Caml-list] Function definition with multiple patterns in multiple equations Message-ID: <20020108082639.A87628@libcom.com> References: <20020108110836.A9147@darling.home.br> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20020108110836.A9147@darling.home.br> User-Agent: Mutt/1.3.22.1i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, Jan 08, 2002 at 11:08:36AM -0200, José Romildo Malaquias wrote: >None of them seems to be supported by the language. In SML, one can write > > fun f 0 0 = 1 > | f _ _ = 0 > >What is the equivalent in OCaml? There is no perfect equivalent in O'Caml. Here, there are two ways to write function definitions. One of them allows multiple arguments to be specified: let f = fun p1 p2 p3 ... -> e (which is equivalent to) let f p1 p2 p3 ... = e The other allows multiple patterns to be specified for one argument: let f = function | p1 -> e1 | p2 -> e2 | p3 -> e3 | ... (which is equivalent to) let f x = match x with | p1 -> e1 | p2 -> e2 | p3 -> e3 | ... At first, I was rather upset with this, but I've come to appreciate it, since it can in fact lead to rather better code for some complex cases by making you think about what you're actually trying to do. Then again, for the very simple cases like yours, it does indeed get in the way a bit. I still feel it may be a better choice, since in SML you can write: fun foo a b = e1 a b | foo c = e1 c (for example.) This, IMO, can lead to some really strange confusion. Anyway, the quick-n-dirty method of doing multiple patterns like your example is to write: let f a b = match (a,b) with | (0, 0) -> 1 | (_, _) -> 0 But, this may very well have the overhead of actually building a pair. You can also decompose it into something like: let f = function | 0 -> (function | 0 -> 1 | _ -> 0) | _ -> 0 Which is, yes, rather unattractive for such a small example. Hope this helps, John. ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr