Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Wolfgang Lux <wlux@uni-muenster.de>
To: flaig@hallucinogene.sanctacaris.net
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] newbie questions
Date: Sun, 30 Mar 2003 16:23:39 +0200	[thread overview]
Message-ID: <33A4A5A2-62BB-11D7-938F-0003937628DA@uni-muenster.de> (raw)
In-Reply-To: <20030329084839.BAF874C99@sitemail.everyone.net>


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


  parent reply	other threads:[~2003-03-30 14:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-29  8:48 Dr.Dr.Ruediger M.Flaig
2003-03-29  9:15 ` Basile STARYNKEVITCH
2003-03-29 11:04 ` Sylvain LE GALL
2003-03-30  9:53 ` Damien Doligez
2003-03-30 14:23 ` Wolfgang Lux [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-12-11  0:25 Jose A. Ortega Ruiz
2001-12-11  2:32 ` Mike Leary
2001-12-11 10:23 ` Sven
2001-08-22 17:58 Collin Monahan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=33A4A5A2-62BB-11D7-938F-0003937628DA@uni-muenster.de \
    --to=wlux@uni-muenster.de \
    --cc=caml-list@inria.fr \
    --cc=flaig@hallucinogene.sanctacaris.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox