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 QAA27248; Sun, 30 Mar 2003 16:23:46 +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 QAA26966 for ; Sun, 30 Mar 2003 16:23:45 +0200 (MET DST) Received: from batch12.uni-muenster.de (BATCH12.UNI-MUENSTER.DE [128.176.188.110]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h2UENdX26397 for ; Sun, 30 Mar 2003 16:23:41 +0200 (MET DST) Received: from zivlnx01.uni-muenster.de (ZIVLNX01.UNI-MUENSTER.DE [128.176.188.24]) by batch12.uni-muenster.de (Postfix) with ESMTP id 6886510C3; Sun, 30 Mar 2003 16:23:32 +0200 (MES) Received: from localhost (localhost.uni-muenster.de [127.0.0.1]) by zivlnx01.uni-muenster.de (Postfix with Virus Detection) with ESMTP id 65BA3312C9; Sun, 30 Mar 2003 16:23:32 +0200 (CEST) Received: from uni-muenster.de (WI-STRUPPI.UNI-MUENSTER.DE [128.176.159.176]) by zivlnx01.uni-muenster.de (Postfix with Virus Detection) with ESMTP id DA292312C6; Sun, 30 Mar 2003 16:23:26 +0200 (CEST) Date: Sun, 30 Mar 2003 16:23:39 +0200 Subject: Re: [Caml-list] newbie questions Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v551) Cc: caml-list@inria.fr To: flaig@hallucinogene.sanctacaris.net From: Wolfgang Lux In-Reply-To: <20030329084839.BAF874C99@sitemail.everyone.net> Message-Id: <33A4A5A2-62BB-11D7-938F-0003937628DA@uni-muenster.de> Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.551) X-Virus-Scanned: by AMaViS 0.3.12pre7 X-Spam: no; 0.00; wlux:01 uni-muenster:01 caml-list:01 newbie:01 recursion:01 haskell:01 erlang:01 slower:01 'g':01 iterative:01 cha:99 recursions:01 implemented:01 arrays:01 caml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Ruediger M.Flaig wrote: > 1.: Is there any means of doing list-type pattern matching (style "| > h::t -> ...") for recursion on strings? OK., I have realized that not > having a decent string type is one of the reason why programs in > Haskell or Erlang are much slower than in CAML, and resorted to a > "roll-your-own" for dealing with this: > > let ht x = (String.sub x 1 ((String.length x)-1)), String.make 1 > (compl x.[0]);; > > because I think that > > let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't' | 't' -> 'a' > | _ -> ' ';; > let rec complement = function > "" -> "" > | dna -> let h, t = ht dna in (complement h) ^ t;; > > is much smarter than the iterative version > > let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't' | 't' -> 'a' > | _ -> ' ';; > let complement dna = > let cdna = ref "" in > for i = 0 to String.length( dna )-1 do > let cha = String.get dna i in > cdna := (String.make 1 (compl cha)) ^ !cdna > done; > !cdna;; > > (Yes! Confess guilty! Learned Pascal in 1986...) > > but -- and this is my point -- there should certainly be some more > elegant way of dealing with this than the horrible "ht" definition, > shouldn't it? Actually, in many cases you would not want to implement such recursions yourself. Many recursion patterns can implemented by using the standard higher order functions unit, map, fold, etc. For instance, your complement function should be as simple as let complement dna = String.map compl dna To my surprise, the String module doesn't provide a map function (it does implement iter, though). If you were using an algebraic data type type Base = A | C | G | T you could make use of arrays and the implement complement using Array.map. Wolfgang ------------------- 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