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=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 76154BBAF for ; Tue, 24 Jun 2008 23:27:44 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoQBAEgDYUjAXQImiGdsb2JhbACSLkMBAQEPIJsxhU0 X-IronPort-AV: E=Sophos;i="4.27,698,1204498800"; d="scan'208";a="14388242" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 24 Jun 2008 23:27:44 +0200 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m5OLRh5t001481 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 24 Jun 2008 23:27:44 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq8AAMADYUhKfSwdemdsb2JhbACSLkMBAQsDBAYHEZsthU0 X-IronPort-AV: E=Sophos;i="4.27,698,1204498800"; d="scan'208";a="12491682" Received: from yx-out-2324.google.com ([74.125.44.29]) by mail2-smtp-roc.national.inria.fr with ESMTP; 24 Jun 2008 23:27:43 +0200 Received: by yx-out-2324.google.com with SMTP id 8so497726yxg.27 for ; Tue, 24 Jun 2008 14:27:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=2o18iY7/amYlCbwn2LezWMDSIllk8mNNyxKbMKtEQN8=; b=T2ZIA2nNE5wNGEUR9Nititpullu9R/MgpmCb8SoX+zO1OLITH7MQSSupYAaxEM4CMp 0c2NOvjTFx1dSgUmjY/UcbwUPFaHbeZo1Ef2UOChO+Ali62qV3pHMBcFF/rkUJTQnOzC 0cHqb69ggYafBtJC70bYc4WGFbMlFUKpJIPHk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=teKFwD6+JCkjBM28N9lnveEzm1BB2t0U8ngC6AvnMlVJWTpsjKiT3tkInJnbjjOSdn eXwMxWj+byIK3+lnFXL6kHeWqLCn68xKopDdLPRfxqm22d81qB7TUdBePv7hjN3mKIAs CYL7hibK+PXe0iYDq5eXrvA71SAKCc4XHjxhI= Received: by 10.142.161.13 with SMTP id j13mr5636468wfe.230.1214342861983; Tue, 24 Jun 2008 14:27:41 -0700 (PDT) Received: by 10.150.145.10 with HTTP; Tue, 24 Jun 2008 14:27:41 -0700 (PDT) Message-ID: Date: Tue, 24 Jun 2008 14:27:41 -0700 From: "Nathaniel Gray" To: "Richard Jones" Subject: Re: [Caml-list] ANN: patterns v0.4 Cc: "Jeremy Yallop" , caml-list@inria.fr In-Reply-To: <20080620162617.GA4530@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <48579E13.8070706@ed.ac.uk> <4858338B.3020103@ed.ac.uk> <20080620162617.GA4530@annexia.org> X-Miltered: at discorde with ID 486166CF.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; syntax:01 syntax:01 off-list:01 cheers:01 0.4:98 20,:98 invoke:01 wrote:01 regex:01 rec:01 rec:01 caml-list:01 functions:01 lazy:02 lazy:02 On Fri, Jun 20, 2008 at 9:26 AM, Richard Jones wrote: > > Can someone summarise active patterns for us? The MSDN site > containing the paper is down at the moment. Sure. Active patterns are a lot like Wadler's "views", and thus are similar to the stuff in micmatch. With active patterns you can invoke functions (implicitly) within pattern matches and match the results, which allows you to provide a "virtual" algebraic data structure for any value you might want to match against. Here's an example from the paper (using F# syntax): open LazyList let (|Cons|Nil|) l = if nonempty(l) then Cons(hd(l),tl(l)) else Nil let rec pairSum xs = match xs with | Cons (x, Cons (y,ys)) -> consl (x+y) (lazy (pairSum ys)) | Cons (x, Nil ()) -> consl x (lazy nil) | Nil () -> nil This expands to: let rec pairSum xs = if nonempty xs then let x, ys = hd xs, tl xs if nonempty ys then let y, zs = hd ys, tl ys consl (x+y) (lazy (pairSum zs)) else consl x (lazy nil) ) else nil There are other variations presented in the paper, including parameterized patterns. These are nice for doing things like regular expression matching. Again, from the paper: let (|ParseRE|_|) re s = let m = Regex(re).Match(s) if m.Success then Some [ for x in m.Groups -> x.Value ] else None let swap s = match s with | ParseRE "(\w+)-(\w+)" [l;r] -> r ^ "-" ^ l (* See below *) | _ -> s The matching syntax here is a bit confusing because it can be hard to tell where parameters end and patterns begin. In the example above, "(\w+)-(\w+)" is a parameter and [l;r] is a pattern. There's definitely room for improvement over this syntax. There are other variations and examples in the paper. I'd definitely recommend reading it. If you still can't get it from the website I can forward you a copy off-list. Cheers, -n8 -- >>>-- Nathaniel Gray -- Caltech Computer Science ------> >>>-- Mojave Project -- http://mojave.cs.caltech.edu -->