caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] New to OCaml: can someone explain how this code can be done better?
@ 2001-06-23  5:27 Jeremy Fincher
  2001-06-23 19:39 ` Eric C. Cooper
  0 siblings, 1 reply; 5+ messages in thread
From: Jeremy Fincher @ 2001-06-23  5:27 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: [Caml-list] New to OCaml: can someone explain how this code can be done better?
@ 2001-06-24  5:49 Jeremy Fincher
  0 siblings, 0 replies; 5+ messages in thread
From: Jeremy Fincher @ 2001-06-24  5:49 UTC (permalink / raw)
  To: caml-list

>The function permute_rec is an implementation detail.
>So you scope it _inside_ the permute function

I was worried this would incur a slow down (in Python, nested function 
objects are instantiated when a function is called and destroyed when it 
exits; the code object isn't destroyed, but there still is the additional 
overhead of creating a function object from it.)  I assume now, since it's a 
feature so widely used (I see it anytime I look at others' OCaml code) that 
it doesn't.  Thanks for pointing that out.

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


^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: [Caml-list] New to OCaml: can someone explain how this code can be done better?
@ 2001-06-24  6:03 Jeremy Fincher
  0 siblings, 0 replies; 5+ messages in thread
From: Jeremy Fincher @ 2001-06-24  6:03 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-06-25 18:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-23  5:27 [Caml-list] New to OCaml: can someone explain how this code can be done better? Jeremy Fincher
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

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