From: "Jeremy Fincher" <tweedgeezer@hotmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] New to OCaml: can someone explain how this code can be done better?
Date: Sat, 23 Jun 2001 01:27:48 -0400 [thread overview]
Message-ID: <F13liS01qCcWNYgEj4v00003e3d@hotmail.com> (raw)
Since I'm learning OCaml, I've been writing little functions as I get a
grasp on each little bit of the syntax. One of these functions is
"permute", which takes a string and returns a list of all the permutations
of the string. Here's the function:
let rec permute_rec f s =
if String.length s = 1 then
(f ^ s) :: []
else
let l = ref [] in
for i = 0 to ((String.length s) - 1) do
let c = String.sub s i 1 in
let rem = (String.sub s 0 i) ^ ((String.sub s (i+1) ((String.length
s)-i-1))) in
l := (List.append !l (permute_rec (f^c) rem))
done;
!l;;
let permute = permute_rec "";;
Now, I'm sure this isn't the best way to code that function. What I'm
hoping is that someone can show me a few things about the function: how to
make it tail recursive; a better (less imperative?) method of writing it,
and which parts are not consistent with common/standard OCaml idiom.
*Anything* I'm shown should at least help me learn, so if you have any
comments/opinions, please tell me!
Thanks,
Jeremy
_________________________________________________________________
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
next reply other threads:[~2001-06-23 5:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-23 5:27 Jeremy Fincher [this message]
2001-06-23 19:39 ` Eric C. Cooper
2001-06-25 18:40 ` Stefano Zacchiroli
2001-06-24 5:49 Jeremy Fincher
2001-06-24 6:03 Jeremy Fincher
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=F13liS01qCcWNYgEj4v00003e3d@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