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 UAA09940; Sun, 24 Oct 2004 20:32:10 +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 UAA09686 for ; Sun, 24 Oct 2004 20:32:09 +0200 (MET DST) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i9OIW77o003080 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Sun, 24 Oct 2004 20:32:08 +0200 Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.13.1/8.12.11) with ESMTP id i9OIW1sW012179 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 24 Oct 2004 13:32:04 -0500 (CDT) Date: Sun, 24 Oct 2004 13:41:11 -0500 (CDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Vasili Galchin cc: caml-list@inria.fr Subject: Re: [Caml-list] Patterns in ML vs OCaml ... In-Reply-To: <20041024173552.96205.qmail@web53002.mail.yahoo.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at nez-perce with ID 417BF527.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 ocaml's:01 beginner's:01 beginners:01 val:01 accu:01 accu:01 referential:01 ocaml:01 ocaml:01 equality:01 equality:01 groups:01 rec:01 rec:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk First off, there's an Ocaml's beginner's list this would be more appropriate for: http://groups.yahoo.com/group/ocaml_beginners/ On Sun, 24 Oct 2004, Vasili Galchin wrote: > > Hello, > > I'm sorry this is a very elementary question (but I am pulling my hair out). With a function written in ML: > > fun filter_list P [] = [] | > filter_list P (h :: t) = let val ft = filter_list P t > in if P h then (h :: ft) > else ft > end; A literal translation of this function into Ocaml would be: let rec filter_list p = function | [] -> [] | h :: t -> let ft = filter_list p t in if p h then h :: ft else ft ;; Of course, the problem with this function is that it's not tail recursive. For sufficiently long lists (say, more than few tens of thousands of elements) you'll blow the stack. A better implementation would be: let filter_list p = let rec loop accu = function | [] -> List.rev accu | h :: t -> if p h then loop (h :: accu) t else loop accu t in loop [] ;; Also note that this function already exists in the standard library with the name List.filter. If this is the actual function you need, use the standard library version instead. > > fun member (e, nil) = false | > member (e , (h :: t)) = if (e=h) then true > else member (e, t) ; > The literal translation of this function would be: let rec member = function | (e, []) -> false | (e, (h :: t)) -> if e = h then (* note: structural comparison *) true else member (e, h) The problem with this function is that every call requires the allocation of a new tuple. A better implementation is probably: let rec member e = function | [] -> false | h :: t -> if e = h then true else member e t ;; Once again, this function exists in the standard library, with the name List.mem (A variant, which uses referential equality instead of structural equality, also exists with the name List.memq). -- "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