Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] New to OCaml: can someone explain how this code can be done better?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jeremy Fincher <tweedgeezer@h...>
Subject: Re: [Caml-list] New to OCaml: can someone explain how this code can be done better?
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