caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jeremy Fincher" <tweedgeezer@hotmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] New to OCaml: can someone explain how this code can be done better?
Date: Sun, 24 Jun 2001 02:03:43 -0400	[thread overview]
Message-ID: <F178OLZCHusMjQHtMFa0001780c@hotmail.com> (raw)

Thanks!  Going through and understanding your code helped me a lot, and was 
a terrific aid in seeing the functional (rather than imperative) way of 
looping.  One question, though:

>     let explode string =
>       let rec loop i acc =
>         if i = 0 then acc
>         else let i' = i-1 in loop i' (string.[i'] :: acc)
>       in
>       loop (String.length string) []

Could this be rewritten as this, to remove the if/else? --

let explode string =
  let rec loop acc = function
    | 0 -> acc
    | i -> loop (string.[i-1] :: acc) (i-1)
  in
  loop [] (String.length string)


Thanks for your excellent examples.

Jeremy
>
>     let implode list =
>       let string = String.create (List.length list) in
>       let rec loop i = function
>         | x :: rest -> string.[i] <- x; loop (i+1) rest
>         | [] -> ()
>       in
>       loop 0 list;
>       string
>
>     let permute_string s =
>       List.map implode (permute (explode s))
>
>(* Eric Cooper, ecc@cmu.edu *)
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

-------------------
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


             reply	other threads:[~2001-06-24  6:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-24  6:03 Jeremy Fincher [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-06-24  5:49 Jeremy Fincher
2001-06-23  5:27 Jeremy Fincher
2001-06-23 19:39 ` Eric C. Cooper
2001-06-25 18:40   ` Stefano Zacchiroli

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=F178OLZCHusMjQHtMFa0001780c@hotmail.com \
    --to=tweedgeezer@hotmail.com \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).