From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.2 required=5.0 tests=AWL,DNS_FROM_AHBL_RHSBL, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 1102ABC6B for ; Wed, 11 Jul 2007 10:44:24 +0200 (CEST) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.247]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l6B8iNUA000560 for ; Wed, 11 Jul 2007 10:44:23 +0200 Received: by an-out-0708.google.com with SMTP id c24so319442ana for ; Wed, 11 Jul 2007 01:44:23 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=tVwTVjdfTvGBKNKk8rqVojHDQKtv66csHKFCnzVPn7FXUr6005kpXsZUovOz4JGh/prJRqgC6BzsMkOTFnUY6WtmMV7dfJiACJWPJVhUFn+ZBJlYgyZXmBYu9wIi4BoiXZRhGlL70xt6AmohsNLegX1lnzk9DNEcFTKnzRK9QT4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=BqwAeWKF5m7Y+ANdy69/uGWZ2OTairVKielOJSh54zXi3zxVnHpikErHXiFo9ceYE950AcHQJUbDF+3ND2QjqCVgegnVsn3ke/4VryrmtGtUDyPQBMvw9KMUAPtuykSHXYPbbSXus/LXZ/TaDaCiVGoU9vM5sjqs0oOTVgrJubQ= Received: by 10.100.189.17 with SMTP id m17mr2659745anf.1184143462899; Wed, 11 Jul 2007 01:44:22 -0700 (PDT) Received: by 10.100.33.15 with HTTP; Wed, 11 Jul 2007 01:44:22 -0700 (PDT) Message-ID: <6f9f8f4a0707110144i2206c20dw22dc67ffa42a4a42@mail.gmail.com> Date: Wed, 11 Jul 2007 10:44:22 +0200 From: "Loup Vaillant" To: caml-list@inria.fr Subject: Re: [Caml-list] Pattern matching over lazy lists In-Reply-To: <200707102338.47010.jon@ffconsultancy.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200707102338.47010.jon@ffconsultancy.com> X-j-chkmail-Score: MSGID : 46949867.000 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 46949867.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 failwith:01 compiler:01 caml-list:01 lazy:02 lazy:02 tuple:02 match:02 match:02 functional:02 output:02 module:03 element:03 pattern:04 pattern:04 (Sorry for the double post) 2007/7/11, Jon Harrop : > What's the best way to do this? > > I was thinking of forcing the first few elements of a lazy list before pattern > matching and then looking for forced values in the lists as patterns but I > don't think you can deconstruct a lazy value in a pattern match... You are talking about streams, right? Not just the suspension of a regular list? Camlp4 provide "functional streams", which are not destructive. If you don't want to use that... ...At least, you can deconstruct the very first suspension : match force l with | Empty -> (****) | Cons x l -> (****) might do it. maybe : match hd (force l) with | 0 -> (****) | x -> (****) would be better for your purpose. However, with more than one element (3 here) : match hd (force l), hd (force (tl (force l))), hd (force (tl (force (tl (force l))))) with | 0, 0, 0 -> (****) | x, y, z -> (****) Quite ugly. In that case, I suggest you write a "peek_n" function which take a stream as input, and provide you a list (or tuple, or whatever) as output, so you can match it directly : match peek_n 3 l with | [0; 0; 0] -> (****) | [x; y; z] -> (****) | _ -> failwith "impossible error" Did I answer your question? Loup Vaillant PS : > I don't think you can deconstruct a lazy value in a pattern match... Why not? A lazy value is a value like any other. (Whoops, I just looked at the Lazy module, which hide some magic... compiler optimizations?)