It would be nice to have metaocaml as one of the available compilers in opam. Anyone thinking of doing that?

On Wed, Dec 19, 2012 at 6:50 PM, Jeremy Yallop <yallop@gmail.com> wrote:
On 19 December 2012 22:23, Philippe Wang <mail@philippewang.info> wrote:
> I have a (somehow silly) answer to your question:
>
> let gen_make_circ n =
>   Printf.printf "let make_circ = function [] -> []\n";

That's an interesting idea, Philippe.  Here's an approach along the
same lines using MetaOCaml.

  let rec docycle l =
   .!(.< let rec s =
          .~(let rec loop = function
               | [] -> .< s >.
               | x :: xs -> .< x :: .~(loop xs) >.
             in loop l)
         in s >.)

The essential idea is the same as in your code: dynamically generating
a suitable 'let rec' expression.  However, MetaOCaml also helpfully
guarantees that the generated code is well-formed and well-typed, and
makes it possible to compile and run the generated code without
leaving the language.

Here's the code in action:

    # docycle;;
    - : 'a list -> 'a list = <fun>
    # docycle [1;2;3];;
    - : int list = [1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2;
3; 1; ...]
    # docycle [1;2;3;4;5];;
    - : int list = [1; 2; 3; 4; 5; 1; 2; 3; 4; 5; 1; 2; 3; 4; 5; 1; 2;
3; 4; ...]

(For the sake of simplicity I haven't handled the case where the input
list is empty, but that's not difficult to do.)

--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs