From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id 719C07F2AA for ; Thu, 20 Dec 2012 16:24:31 +0100 (CET) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of agarwal1975@gmail.com) identity=pra; client-ip=209.85.223.177; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="agarwal1975@gmail.com"; x-sender="agarwal1975@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.national.inria.fr: domain of agarwal1975@gmail.com designates 209.85.223.177 as permitted sender) identity=mailfrom; client-ip=209.85.223.177; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="agarwal1975@gmail.com"; x-sender="agarwal1975@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ie0-f177.google.com) identity=helo; client-ip=209.85.223.177; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="agarwal1975@gmail.com"; x-sender="postmaster@mail-ie0-f177.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvIBAIgs01DRVd+xm2dsb2JhbABEqx+JPgGJCAgWDgEBAQEBCAkLCRQngh4BAQQBJxkBGxILAQMBCwYFBAcNDSEhAQERAQUBChIGExKHbgEDCQYMmjqMM4J7hSMKGScDClmIdgEFDItdaRuEKAOIYotTgVaBHIobgzEWKYQzgU8 X-IronPort-AV: E=Sophos;i="4.84,324,1355094000"; d="scan'208";a="166431108" Received: from mail-ie0-f177.google.com ([209.85.223.177]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Dec 2012 16:24:29 +0100 Received: by mail-ie0-f177.google.com with SMTP id k13so4753852iea.22 for ; Thu, 20 Dec 2012 07:24:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=w2SwfA/NeoQK/+7wqUzP+Va0ccRV7Iq1C4xrsKGKh48=; b=dGumnHUOByQhHeTCD3JK/+j4gwHxBF3hfeiBauZ1NC1C84XjdJnZjtvH2MlydhcBya HdqA0PTB94qE9J8bzvkxMnhGWGbOlXJ5GdXwljXIqLYnFvToAFLOIuUsfr+f+MgWLO1X jH/nswEyARVL4wYkpJO2UzwFdT8wWbhvwaTbBZXkXDXDqGQZv2tMnXk0AncC57TAo4VB mW9yQ/bx13ZOjAGaWMCsNnxuA5VoBzkL6dvSFG3Kvl+GUT3aD41vmGch017HFKG7rSvg r2tzKU3e85sjX1GzNtpER6EQm9dooF4wFaZDzGOCp74BTwNR0PtoNY5QVFYWCf/75HR8 vIkw== Received: by 10.50.220.199 with SMTP id py7mr10981930igc.34.1356017068616; Thu, 20 Dec 2012 07:24:28 -0800 (PST) MIME-Version: 1.0 Received: by 10.64.47.229 with HTTP; Thu, 20 Dec 2012 07:24:08 -0800 (PST) In-Reply-To: References: <50d02b62.827bc20a.6f6e.65b8SMTPIN_ADDED_BROKEN@mx.google.com> From: Ashish Agarwal Date: Thu, 20 Dec 2012 10:24:08 -0500 Message-ID: To: Jeremy Yallop Cc: Caml List , Eric Jaeger , Philippe Wang Content-Type: multipart/alternative; boundary=f46d0434bdd2af2d2f04d14a53f4 Subject: Re: [Caml-list] Function returning recursive lists --f46d0434bdd2af2d2f04d14a53f4 Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > On 19 December 2012 22:23, Philippe Wang 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 = > # 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 > --f46d0434bdd2af2d2f04d14a53f4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable It would be nice to have metaocaml as one of the available compilers in opa= m. Anyone thinking of doing that?

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

That's an interesting idea, Philippe. =A0Here's an approach a= long the
same lines using MetaOCaml.

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

The essential idea is the same as in your code: dynamically generating
a suitable 'let rec' expression. =A0However, MetaOCaml also helpful= ly
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:

=A0 =A0 # docycle;;
=A0 =A0 - : 'a list -> 'a list =3D <fun>
=A0 =A0 # docycle [1;2;3];;
=A0 =A0 - : int list =3D [1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2= ;
3; 1; ...]
=A0 =A0 # docycle [1;2;3;4;5];;
=A0 =A0 - : int list =3D [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<= br> list is empty, but that's not difficult to do.)

--
Caml-list mailing list. =A0Subscription management and archives:
ht= tps://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

--f46d0434bdd2af2d2f04d14a53f4--