caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nathaniel Gray" <n8gray@gmail.com>
To: "Richard Jones" <rich@annexia.org>
Cc: "Jeremy Yallop" <jeremy.yallop@ed.ac.uk>, caml-list@inria.fr
Subject: Re: [Caml-list] ANN: patterns v0.4
Date: Tue, 24 Jun 2008 14:27:41 -0700	[thread overview]
Message-ID: <aee06c9e0806241427t26316fa3nead7ac0e3852ca46@mail.gmail.com> (raw)
In-Reply-To: <20080620162617.GA4530@annexia.org>

On Fri, Jun 20, 2008 at 9:26 AM, Richard Jones <rich@annexia.org> 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 -->


      parent reply	other threads:[~2008-06-24 21:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-17 11:20 Jeremy Yallop
2008-06-17 21:37 ` [Caml-list] " Nathaniel Gray
2008-06-17 21:58   ` Jeremy Yallop
2008-06-20 16:26     ` Richard Jones
2008-06-20 16:56       ` Martin Jambon
2008-06-24 21:27       ` Nathaniel Gray [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aee06c9e0806241427t26316fa3nead7ac0e3852ca46@mail.gmail.com \
    --to=n8gray@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=jeremy.yallop@ed.ac.uk \
    --cc=rich@annexia.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).