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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id E52B97F2AA for ; Thu, 20 Dec 2012 00:50:22 +0100 (CET) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of yallop@gmail.com) identity=pra; client-ip=209.85.212.180; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of yallop@gmail.com designates 209.85.212.180 as permitted sender) identity=mailfrom; client-ip=209.85.212.180; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wi0-f180.google.com) identity=helo; client-ip=209.85.212.180; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="postmaster@mail-wi0-f180.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoUBAJxR0lDRVdS0k2dsb2JhbABEhjq3OAgWDgEBAQEJCQsJFAQjgh4BAQUjBBkBGx0BAwwGBQsNAgImAgIiAREBBQEcBhOIAAEDD5lYi2RPgnuFFwoZJw1ZiHYBBQyBFosrG4MVgRMDlgqOaBYphBWBbQ X-IronPort-AV: E=Sophos;i="4.84,320,1355094000"; d="scan'208";a="186937825" Received: from mail-wi0-f180.google.com ([209.85.212.180]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Dec 2012 00:50:22 +0100 Received: by mail-wi0-f180.google.com with SMTP id hj13so1620333wib.13 for ; Wed, 19 Dec 2012 15:50:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=oJnrEkWCM5V1HTn46+rABEAM2YDGI5nvpIxrEMMi8J4=; b=AljWKZJsdAl62tojJWINC8+SxfUS5GEVKa2wYBKOoS971T7o9tn0UNXvgHbf0lsyX/ EN6k+DVxMlAY2G0h2dLt409yr+DdGsp0WNdlqS8A2I85joTIBYkiOOT6E6e5KL/mJoVe T2+V8l1c28jicjHKuiyCWwcEaueocoR64sLL6Tj+uEIZk5XnCSF3IjVmFU/y7/FAbSYg HGA+RS0WD5CBS98EpDgiXHVXm+cBDvHmMAsUhDtUPaaqTBLlD556AXh5Nz7SptftxorE nbg7R11ZqCPVyBUpA5MF+mBsCN/U/rw+LKRLZpCJfCuMBChwRLc+kOyIYjbL1ylIVg7S YWKA== MIME-Version: 1.0 Received: by 10.194.236.68 with SMTP id us4mr14597508wjc.11.1355961022120; Wed, 19 Dec 2012 15:50:22 -0800 (PST) Received: by 10.194.58.101 with HTTP; Wed, 19 Dec 2012 15:50:22 -0800 (PST) In-Reply-To: References: <50d02b62.827bc20a.6f6e.65b8SMTPIN_ADDED_BROKEN@mx.google.com> Date: Wed, 19 Dec 2012 23:50:22 +0000 Message-ID: From: Jeremy Yallop To: Caml List Cc: Eric Jaeger , Philippe Wang Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Function returning recursive lists 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.)